【问题标题】:Significant Location changes not firing properly when device locked设备锁定时,重大位置更改无法正常触发
【发布时间】:2015-04-25 07:14:56
【问题描述】:

我试图弄清楚为什么在设备锁定时使用重大位置更改时不会触发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];
    }

【问题讨论】:

    标签: ios location core-location


    【解决方案1】:

    首先,我认为您可能不会开始跟踪应用程序是否启动,而是在其他地方,当应用程序已经启动时,您可以检查您的应用程序是否已被授权

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

    你的代表应该等待这个

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

    您还需要将NSLocationAlwaysUsageDescription 添加到您的 Info.plist 文件中,否则将永远不会提示用户授权您的应用,一旦您的应用获得授权,您就可以开始跟踪设备

    我不得不提到 locationManager 在其他地方,而不是 AppDelegate 中的属性,我使用了一个静态变量,可以通过单例访问,但您可能有另一种访问方式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 2016-05-29
      • 2012-01-23
      • 2013-02-14
      • 2019-11-24
      相关资源
      最近更新 更多