位置服务无法在WatchKit调用的后台运行

时间:2015-05-14 15:10:49

标签: ios background watchkit location-services

我的Apple Watch应用程序需要一些数据并从相应的iPhone应用程序请求它。要满足请求,iPhone应用程序需要用户位置。 在使用真正的Apple Watch接收和测试后,我发现我的iPhone应用程序在后台运行时没有收到位置更新。如果iPhone应用程序在前台处于活动状态,则可以正常运行。使用模拟器,它在两种情况下都有效。

在这两种情况下(活动和后台),WatchKit扩展程序会成功调用并启动iPhone应用程序并一直运行,直到在iPhone应用程序中调用startUpdatingLocation。但是,如果应用程序在后台运行,则永远不会调用didUpdateLocations。

我尝试使用requestAlwaysAuthorization以及requestWhenInUseAuthorization。没有不同。 我还激活了"位置更新"功能内的后台模式。但同样没有区别。

是否其他人遇到了同样的问题并找到了在后台接收该位置的方法?

这里有一些代码。首先检查是否需要授权。

// iOS 8 check to avoid crash on older iOS
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
    {
        [self requestLocationAlwaysAuthorization];
    }
    else
    {
        [self runLocationUpdate];
    }

此处检查适当的位置管理员权限。

- (void)requestLocationAlwaysAuthorization
{
CLAuthorizationStatus currentAuthStatus = [CLLocationManager authorizationStatus];

if (currentAuthStatus == kCLAuthorizationStatusDenied)
{
    //request user to change setting
}
else if (currentAuthStatus == kCLAuthorizationStatusRestricted)
{
    //request user to change setting
}
else if (currentAuthStatus == kCLAuthorizationStatusNotDetermined)
{
    [self.locationManager requestAlwaysAuthorization];

    [self runLocationUpdate];
}
else if (currentAuthStatus == kCLAuthorizationStatusAuthorizedWhenInUse)
{
    //maybe when in use is also enough?
    [self runLocationUpdate];
}
else if (currentAuthStatus == kCLAuthorizationStatusAuthorizedAlways)
{
    //all ok
    [self runLocationUpdate];
}

}

这里调用startUpdatingLocation。只有在iPhone应用程序处于活动状态时才会调用didUpdateLocations委托。

-(void)runLocationUpdate
{
    [self.locationManager startUpdatingLocation];
}

1 个答案:

答案 0 :(得分:3)

要检查和注意的三件事:

  1. 位置[self.locationManager requestAlwaysAuthorization];等权限仅由操作系统确认一次。如果您已经请求了权限,则无论级别如何,操作系统都不会向用户显示请求。操作系统将仅传递请求并保留权限级别。如果[CLLocationManager authorizationStatus]返回kCLAuthorizationStatusNotDetermined,则唯一可以确保操作系统向用户显示请求的时间。在所有其他情况下,您必须通过显示警报或其他形式的UI显示来手动请求权限。另请注意,即使您删除了应用并重新安装,操作系统仍会保留其是否已显示请求。因此,要进行测试,您需要重置模拟器的内容或iPhone的位置隐私。

  2. 确保您已为NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription添加了plist密钥如果您不将其添加到plist,操作系统将忽略任何位置权限请求。

  3. 如果您想在手机应用在后台时使用requestAlwaysAuthorization从手机(而不是手表应用扩展程序)获取位置数据,还需要您注册背景模式下的位置更新项目>目标>功能

  4. <强>更新 使用后台任务可以让您的应用有时间在后台进行响应。像这样:

    -(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *replyInfo))reply{
    
        UIApplication *app = [UIApplication sharedApplication];
    
        UIBackgroundTaskIdentifier bgTask __block = [app beginBackgroundTaskWithName:@"watchAppRequest" expirationHandler:^{
    
            [[UIApplication sharedApplication] endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
    
        }];
    
    //make your calls here to your tasks, when finished, send the reply then terminate the background task
    
    //send reply back to watch
    reply(replyInfo);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            [app endBackgroundTask:bgTask];
            bgTask=UIBackgroundTaskInvalid;
        });
    }