I have found the following solution explaining how to utilize location services in iOS 8. Unfortunately I have got only an iPhone 6 simulator and not the real device. I tried to add this code in the viewDidLoad method but the authorization dialog is never shown. Why is this?
I added a breakpoint in the following, it gets executed but does not show the dialouge..:
// Will open an confirm dialog to get user's approval
[locationManager requestAlwaysAuthorization];
And here is the full viewDidLoad method:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize location manager
locationManager = [CLLocationManager new];
locationManager.delegate = self;
float system = [[[UIDevice currentDevice] systemVersion] floatValue ];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&
[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways
) {
// Will open an confirm dialog to get user's approval
[locationManager requestAlwaysAuthorization];
} else {
[locationManager startUpdatingLocation]; //Will update location immediately
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
PS: I also modified the info.plist as following:
PPS:
This is how I implement (in the same ViewController) the rest of the CLLocationManagerDelegate protocol:
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
NSLog(@"User still thinking..");
} break;
case kCLAuthorizationStatusDenied: {
NSLog(@"User dislikes your app");
} break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
case kCLAuthorizationStatusAuthorizedAlways: {
[locationManager startUpdatingLocation];
} break;
default:
break;
}
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);
}
EDIT 2: I added the following block of code as suggested in an answer. The code is executed but dialogue fails to appear.
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined: {
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
[locationManager requestAlwaysAuthorization];
}
} break;
case kCLAuthorizationStatusDenied: {
NSLog(@"User dislikes your app");
} break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
case kCLAuthorizationStatusAuthorizedAlways: {
[locationManager startUpdatingLocation];
} break;
default:
break;
}
}