Using CLLocationManager in iOS8/9/10

iOS8 之後取得授權的方式有所改變。

iOS7:

[_locationManager startUpdatingLocation];

iOS8:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
  [_locationManager requestAlwaysAuthorization];
//  [_locationManager requestWhenInUseAuthorization];
}
#endif
[_locationManager startUpdatingLocation];

iOS9:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
    [_locationManager requestAlwaysAuthorization];
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
    _locationManager.allowsBackgroundLocationUpdates = YES;
}

[CLLocationManager authorizationStatus]:

// iOS7
kCLAuthorizationStatusAuthorized
// iOS8
kCLAuthorizationStatusAuthorizedWhenInUse
kCLAuthorizationStatusAuthorizedAlways

You have to update the app's Info.plist file for iOS8.

<key>NSLocationWhenInUseUsageDescription</key>
<string>Hello World</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Hello World</string>

Enable Background Mode for iOS9

target -> Capabilities -> Background Modes -> open the background Modes