2015-10-16 77 views
0

我使用iBeacon顯示模板例如,在我使用本地notifications.when應用不支持後臺「didExitRegion」「didEnterRegion」「didRangeBeacons」方法獲取調用隨機。我不清楚這些方法在後臺和後臺處理時如何工作,任何人都可以在此協助我。提前感謝。關於應用程序在後臺或從後臺死亡時ibeacons的行爲?

這是示例代碼我使用:

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region { 
     [manager stopRangingBeaconsInRegion:(CLBeaconRegion*)region]; 
     [self.locationManager stopUpdatingLocation]; 
     NSLog(@"You exited the region."); 
     [self sendLocalNotificationWithMessage:@"You exited the region."]; 
} 

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region { 
    [manager startRangingBeaconsInRegion:(CLBeaconRegion*)region]; 
    [self.locationManager startUpdatingLocation]; 
    NSLog(@"You entered the region."); 
    [self sendLocalNotificationWithMessage:@"You entered the region."]; 
} 

-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region { 
    NSString *message = @"i am in 3 meters."; 
    IMViewController *viewController = (IMViewController*)self.window.rootViewController; 
    viewController.beacons = beacons; 
    [viewController.tableView reloadData]; 

    if(beacons.count > 0) { 
     CLBeacon *nearestBeacon = beacons.firstObject; 
     if(nearestBeacon.proximity == self.lastProximity || 
     nearestBeacon.proximity == CLProximityUnknown) 
     { 
     return; 
     } 
    self.lastProximity = nearestBeacon.proximity; 
    NSLog(@"lastProximity: %ld", (long)self.lastProximity); 
    NSInteger str=(int)nearestBeacon.accuracy; 
    //NSString *distance=[NSString stringWithFormat:@"Distance: %d",(int)nearestBeacon.accuracy]; 
    if (str ==3) 
    { 
     [self sendLocalNotificationWithMessage:message]; 
    }  
    } 
} 
+0

監測:進入/退出區域範圍時觸發的動作;在前臺,背景以及應用程序被殺害時都能工作。 測距:基於接近信標觸發的動作;只在前臺工作。試試這個教程http://www.raywenderlich.com/66584/ios7-ibeacons-tutorial –

回答

0

我希望這會給你答案。

如果您讓重大更改的位置服務繼續運行,並且您的iOS應用程序隨後被暫停或終止,則服務會在新位置數據到達時自動喚醒您的應用程序。 在起牀時間,應用程序被置於後臺,您會得到少量時間(大約10秒鐘)以手動重新啓動位置服務並處理位置數據。 (您必須在任何待處理的位置更新可以交付之前,在後臺手動重啓位置服務)。

因爲你的應用程序在後臺,它必須做最少的工作,並避免任何任務(10秒左右),也可以使用beginBackgroundTaskWithName要求更多的後臺執行時間:expirationHandler:UiApplication類的方法

注意:當用戶全局或爲您的應用程序禁用後臺應用程序刷新設置時,重大更改位置服務不會啓動您的應用程序。此外,當後臺應用程序刷新關閉時,即使應用程序處於前臺,應用程序也不會收到重大更改或區域監視事件。

因此,當用戶進入/退出區域時,同樣的情況下也會啓動應用程序。

所以請記住兩件事

  1. 打開後臺應用刷新設置。
  2. 使用beginBackgroundTaskWithName:expirationHandler如果您的代碼需要超過5〜10秒。
相關問題