Сначала включите делегата:
CLLocationManagerDelegate
Объявление свойства:
@ свойство (сильное, неатомное) CLLocationManager * locationManager;
В поле зрения загрузки добавлено следующее:
if( _locationManager == nil )
{
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];
}
[self checkLocationAccess];
И метод checkLocationAccess:
- (void)checkLocationAccess {
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
switch (status) {
// custom methods after each case
case kCLAuthorizationStatusDenied:
[self allowLocationAccess]; // custom method
break;
case kCLAuthorizationStatusRestricted:
[self allowLocationAccess]; // custom method
break;
case kCLAuthorizationStatusNotDetermined:
break;
case kCLAuthorizationStatusAuthorizedAlways:
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
break;
}
}
Метод наблюдателя:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
NSLog(@"allowed"); // allowed
}
else if (status == kCLAuthorizationStatusDenied) {
NSLog(@"denied"); // denied
}
}