Reputation: 374
I would like to ask the user for his permission when the app is loaded, but to get the location later on. I realise this can be done in iOS 8, but in iOS 7 as far as I have researched permission will be asked for automatically when you start updating location. So in iOS 8
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
{
[self.locationManager requestWhenInUseAuthorization];
}
and then you can get the location, but in iOS 7 authorisation is required only on
[locationManager startUpdatingLocation];
Upvotes: 1
Views: 189
Reputation: 58087
EDIT 2: Have a look at this NSHipster article.
It's important to note that you're not going to have identical behaviors with different APIs. The iOS 8 API was designed to be different.
If you want your app to appear to behave identically on both iOS 7
and iOS 8
, you can show some UI to inform the user that they need to enable location. In my apps, I do this with a button in a welcome walkthrough. You might put this button in a menu or whatever.
When the user taps the button, I handle the logic flow as I described above, except if the authorization changes, I'll begin location updates on iOS 8
, instead of ending them.
if (iOS8) {
// Request permission
}
else {
// Begin updates - implicitly ask for permission
}
// .... In authorization did change:
[manager startUpdatingLocation]; // By now, both versions have permission and are ready to update.
This way, you have the same behavior, even if the code paths are a little different.
I have a class that handles all of the logic for me, and it can behave as a singleton or as individual instances. Have a look at it on GitHub (link) if you'd like.
Original Answer:
If you are concerned about battery, I'd suggest triggering updates on iOS 7, and then disabling location when your permissions change, or when you get a location.
From the CLLocationManagerDelegate
documentation:
This method is called whenever the application’s ability to use location services changes. Changes can occur because the user allowed or denied the use of location services for your application or for the system as a whole.
So do something like this:
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
{
[self.locationManager requestWhenInUseAuthorization];
}
else
{
[self.locationManager startUpdatingLocation];
}
Then, in your locationManager:didChangeAuthorizationStatus:
implementation, you can call stopUpdatingLocation.
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status { [manager stopUpdatingLocation]; }
You might even "get away" with no location updates being successfully triggered that way. It's unclear why you want to defer location updates, but this is one way to do it.
Upvotes: 2
Reputation: 284
Why don't you simply use:
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
[self.locationManager requestWhenInUseAuthorization];
This way, you don't need to know (or care) what operating system is being used. Your only concern is whether or not the selector (or class) you want to implement is supported.
Upvotes: 0
Reputation: 8741
Just call startUpdatingLocation when you launch. When you get a location just stop updating.
Upvotes: 0