iOS8 : Blue bar "is Using Your Location" appears shortly after exiting app

标签 ios objective-c core-location

我想在后台跟踪时获得蓝条,但不是。

我的应用在事件时始终使用定位服务,因此在 iOS8 中,我在 CLLocationManager 上使用 requestWhenInUseAuthorization。通常,当您关闭应用程序时,应用程序会停止跟踪您的位置,但用户可以选择让应用程序在后台跟踪他的位置的选项。因此,我在 Info.plist 文件中有 UIBackgroundModeslocation 选项。这非常有效:当切换到后台时,应用程序会不断获取位置更新,并且会出现一个蓝条,提醒应用程序正在使用位置服务。一切都很完美。

但问题是,当用户没有选择在后台跟踪时,蓝条也会出现。在这种情况下,我只需在进入后台时停止来自 AppDelegate 的位置更新:

- (void) applicationDidEnterBackground:(UIApplication *)application
{
    if (!trackingInBackground) {
        [theLocationManager stopUpdatingLocation];
    }
}

蓝条在关闭应用程序后仅显示一秒钟,但看起来仍然很烦人。

我知道使用 requestAlwaysAuthorization 而不是 requestWhenInUseAuthorization 可以解决问题,但是我根本不会得到任何蓝条,在后台跟踪时也不会实际开启。

我已经在 applicationWillResignActive: 方法中尝试过 stopUpdatingLocation,但这没有任何区别。

有谁知道在后台跟踪时如何获得蓝条,但不知道时不知道?

最佳答案

我们经常向 Apple 报告事情,有时他们实际上会采取行动。正如问题中所述,为了防止蓝条很快出现,Apple 在 iOS-9 中的 CLLocationManager 上引入了一个新属性。在您知道需要在后台使用该位置的那一刻进行设置:

theLocationManager.allowsBackgroundLocationUpdates = YES;

或者,以向后兼容的方式:

if ([theLocationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
    [theLocationManager setAllowsBackgroundLocationUpdates:YES];
}

在 swift 中:

if #available(iOS 9.0, *) {
    theLocationManager.allowsBackgroundLocationUpdates = true 
}

如果未设置此属性,则退出应用时不会出现蓝条,并且您的应用无法使用用户位置。 请注意,在 iOS 9 中,您必须设置属性才能在后台使用位置信息。

the WWDC video苹果的解释。

关于iOS8 : Blue bar "is Using Your Location" appears shortly after exiting app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27132698/

相关文章:

iphone - localizedCaseInsensitiveCompare : and caseInsensitiveCompare:? 有什么区别

iphone - 当我从经度和纬度创建位置字符串时,为什么在 iOS 中会出现此链接器错误?

ios - 基于应用程序状态在显着位置变化监控和位置更新监控之间切换

ios - UIWebView iPad/iPhone 不显示登录警报

ios - 每行可变 NSTextCell 宽度/计数——这可能吗?

ios - 合成目的

iphone - 如何使用给定的用户位置初始化 MKMapView?

ios - 如何完全以编程方式添加 UINavigationBar?

ios - 不知道如何使用从 Web 服务返回的 JSON 数据

ios - 从 LinkedIn API 获取电子邮件地址