iOS8:蓝色栏“正在使用您的位置”退出应用后不久出现 [英] iOS8: Blue bar "is Using Your Location" appears shortly after exiting app

查看:648
本文介绍了iOS8:蓝色栏“正在使用您的位置”退出应用后不久出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to get the blue bar when tracking in the background, but not when not.

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

My app uses location services all the time when active, so in iOS8 I use the requestWhenInUseAuthorization on CLLocationManager. Normally, the app stops tracking your location when you close it, but the user can choose the option to let the app track his location in the background as well. Therefore, I have the location option for UIBackgroundModes in the Info.plist file. That works perfectly: when switching to the background, the app keeps getting location updates, and a blue bar appears as a reminder that the app is using location services. All perfect.

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

But the problem is, that the blue bar appears also when the user has not chosen to track in the background. In that case I simply stop location updates from the AppDelegate when entering the background:

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

蓝条在关闭后仅显示一秒钟该应用程序,但它仍然看起来非常恼人。

The Blue bar is shown only for a second after closing the app, but it still looks pretty irritating.

我知道使用 requestAlwaysAuthorization 而不是 requestWhenInUseAuthorization 将解决问题,但是我根本不会得到任何蓝色条,也不会在背景中的跟踪实际开启时。

I know that using requestAlwaysAuthorization instead of requestWhenInUseAuthorization will solve the issue, but then I will not get any blue bar at all, also not when tracking in the background is actually on.

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

I have tried to stopUpdatingLocation already in the applicationWillResignActive: method, but that makes no difference.

有人知道在后台跟踪时如何获得蓝条,但不是在没有的时候吗?

Does anybody know how to get the blue bar when tracking in the background, but not when not?

推荐答案

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

We frequently report things to Apple, and sometimes they actually act upon it. Exactly to prevent the blue bar from appearing shortly as described in the question, Apple has introduced a new property on CLLocationManager in iOS-9. Set it at the moment you know you will need the location in the background:

theLocationManager.allowsBackgroundLocationUpdates = YES;

或以向后兼容的方式:

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

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

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

If this property is not set, then the Blue Bar will not appear when exiting the app, and the user location is not available to your app. Note that in iOS 9, you must set the property in order to be able to use location in the background.

有关Apple的解释,请参阅 WWDC视频

See the WWDC video for Apple's explanation.

这篇关于iOS8:蓝色栏“正在使用您的位置”退出应用后不久出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆