- (
void
)startTrackingLocation {
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if
(status == kCLAuthorizationStatusNotDetermined) {
[_locationManager requestWhenInUseAuthorization];
}
else
if
(status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
[_locationManager startUpdatingLocation];
}
}
- (
void
)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
switch
(status) {
case
kCLAuthorizationStatusAuthorizedAlways:
case
kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@
"Got authorization, start tracking location"
);
[self startTrackingLocation];
break
;
case
kCLAuthorizationStatusNotDetermined:
[_locationManager requestWhenInUseAuthorization];
default
:
break
;
}
}
|
使用时
1
2
3
4
5
|
if
(isIOS8) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[self startTrackingLocation];
}
|
另外,在项目的plist文件里设置key, 可以自定义授权弹窗的提示语。