2015-02-23 54 views
3

我想知道爲什麼locationManager:didUpdateLocations:在設備被鎖定時使用重要位置更改時未被觸發。當設備被鎖定時,重要的位置更改不能正確觸發

到目前爲止locationManager:didUpdateLocations:被一個新位置觸發只有在按下主頁按鈕喚醒設備。

我使用的是iOS 8.1,但不知道這是否是正常行爲。

這是我的代碼(AppDelegate.m):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
     if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]){ 
      NSLog(@"LAUNCHED BY LOCATION UPDATE"); 
     } 
     [self startLocationTrack]; 
    } 

     -(void)startLocationTrack 
    { 
     if (_locationManager == nil) { 
      _locationManager = [[CLLocationManager alloc] init]; 
      _locationManager.delegate = self; 
      _locationManager.pausesLocationUpdatesAutomatically = NO; 
      _locationManager.activityType = CLActivityTypeAutomotiveNavigation; 
      _locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
      if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
       [_locationManager requestAlwaysAuthorization]; 
      } 
      [_locationManager startMonitoringSignificantLocationChanges]; 
     }else{ 
      [_locationManager startMonitoringSignificantLocationChanges]; 
     } 
    } 

    - (void)locationManager:(CLLocationManager *)manager 
     didUpdateLocations:(NSArray *)location 
    {  

     UILocalNotification *notification = [[UILocalNotification alloc] init]; 
     [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:5]]; 
     notification.timeZone = [NSTimeZone defaultTimeZone]; 
     [notification setSoundName:UILocalNotificationDefaultSoundName]; 
     notification.alertBody = @"YOU HAVE MOVE A SIGNIFICANT DISTANCE!!"; 
     notification.alertAction = NSLocalizedString(@"Read Msg", @"Read Msg"); 
     notification.applicationIconBadgeNumber=0; 
     notification.repeatInterval=0; 
     [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
    } 

回答

1

首先,我想你可能無法啓動應用跟蹤並啓動,相反,在其他地方,當應用程序已經啓動,您可以檢查如果您的應用程序已被授權或不

if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) 
    [locationManager requestAlwaysAuthorization]; 

而且您的代理應該等待這個

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status 
{ 
    if (status == kCLAuthorizationStatusAuthorizedAlways) { 
     [locationManager startMonitoringSignificantLocationChanges]; 
    } 
} 

您還需要將NSLocationAlwaysUsageDescription添加到您的Info.plist文件中,否則將永遠不會提示用戶授權您的應用程序,一旦您的應用程序已被授權,您可以開始跟蹤設備

我必須指出locationManager是某處否則,而不是作爲AppDelegate中的屬性,我使用了一個靜態變量,可以通過單例訪問,但是您可能有其他方法來訪問

相關問題