Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

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

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

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.

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.

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.

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?

question from:https://stackoverflow.com/questions/27132698/ios8-blue-bar-is-using-your-location-appears-shortly-after-exiting-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

or, in a backward compatible way:

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

in Swift:

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

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.

See the WWDC video for Apple's explanation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...