2015-10-20 79 views
1

我有一個導航應用程序,使用UILocalNotification時獲取位置更新。UILocalNotification在後臺 - iOS 9

當應用程序轉到iOS 9設備上的背景(鎖定屏幕/按下主頁按鈕)時,它將停止接收通知。適用於iOS 8設備。

我收到通知時,應用程序在前臺,但不是在後臺(當然,這是更重要的)。

代碼看起來是這樣的:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations 
{ 
    CLLocation *currLocation = [locations lastObject]; 

    CLLocationDistance dist = [currLocation distanceFromLocation:[[CLLocation alloc] initWithLatitude:self.coordinate.latitude longitude:self.coordinate.longitude]]; 

    BOOL gotToDestination = dist < 60; 

    if (gotToDestination) { 
     UILocalNotification* n1 = [[UILocalNotification alloc] init]; 
     n1.alertBody = [NSString stringWithFormat:@"You have arrived!"]; 
     n1.soundName = UILocalNotificationDefaultSoundName; 
     n1.fireDate = [NSDate date]; 
     [[UIApplication sharedApplication] presentLocalNotificationNow:n1]; 
    } 
} 
  • 我一直要求授權
  • 我已經檢查的背景模式 - >位置更新
  • 我已經註冊的用戶通知的

將感謝您的幫助。

回答

1

好的,我明白了。

問題不在UILocalNotification,但與位置經理。

從iOS 9開始,CLLocationManager有一個新的BOOL屬性 - allowsBackgroundLocationUpdates,默認爲NO。

如果您想繼續獲取位置更新,您可能需要將其設置爲YES。

這解決了我的問題。

+1

請注意,您的應用可能會在30秒後被暫停或殺死。爲了防止它,你應該明確地詢問iOS以允許後臺執行:https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html。有趣的,但iOS模擬器不會殺死你的應用程序,而真正的設備。 – user996142

+0

謝謝,我知道這一點,但它對未來的讀者有好處:) – chents