This gist outlines the approach for adding a method to Geolocation.m to allow developers to request permissions in iOS8.
Before you start there are a few things I need to mention.
- Once you ask for a permission, you cannot seem to change it at an app level
- The tiapp.xml must have either the NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription keys when requesting the associated permission
Making the update. Add the below to your 3.4.X branch of the Ti SDK
In Geolocation.h add this under the tempManager line
CLLocationManager *locationPermissionManager; // used for just permissions requests
In Geolocation.m add this under the AUTHORIZATION_WHEN_IN_USE line
First we add the below so we can hook a CLLocationManager for this purpose
-(CLLocationManager*)locationPermissionManager
{
if (locationPermissionManager!=nil)
{
// if we have an instance, just use it
return locationPermissionManager;
}
if (locationPermissionManager == nil) {
locationPermissionManager = [[CLLocationManager alloc] init];
locationPermissionManager.delegate = self;
}
return locationPermissionManager;
}
Then we we add the method that could be called via the Ti SDK
-(void)requestAuthorization:(id)value
{
ENSURE_SINGLE_ARG(value,NSNumber);
if (![TiUtils isIOS8OrGreater]) {
return;
}
int requested = [value integerValue];
int currentPermissionLevel = [CLLocationManager authorizationStatus];
if(requested == kCLAuthorizationStatusAuthorizedWhenInUse){
if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]){
if((currentPermissionLevel ==kCLAuthorizationStatusAuthorizedAlways) ||
(currentPermissionLevel ==kCLAuthorizationStatusAuthorized)){
NSLog(@"[ERROR] cannot change already granted permission from AUTHORIZATION_ALWAYS to AUTHORIZATION_WHEN_IN_USE");
}else{
[[self locationPermissionManager] requestWhenInUseAuthorization];
}
}else{
NSLog(@"[ERROR] the NSLocationWhenInUseUsageDescription key must be defined in your tiapp.xml in order to request this permission");
}
}
if((requested == kCLAuthorizationStatusAuthorizedAlways) ||
(requested == kCLAuthorizationStatusAuthorized)){
if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
if(currentPermissionLevel == kCLAuthorizationStatusAuthorizedWhenInUse){
NSLog(@"[ERROR] cannot change already granted permission from AUTHORIZATION_WHEN_IN_USE to AUTHORIZATION_ALWAYS");
}else{
[[self locationPermissionManager] requestAlwaysAuthorization];
}
}else{
NSLog(@"[ERROR] the NSLocationAlwaysUsageDescription key must be defined in your tiapp.xml in order to request this permission");
}
}
}
You will notice a fare amount of error handling is added. This is due to how Apple handles things.