開啟定位步驟
[TOC]
第一步: 開啟后臺(tái)模式,選中定位選擇project –> capabilities–>Backgorund Modes –> Location updates 如圖:
第二步: 在info.list 文件中添加如下配置:(添加定位權(quán)限污桦,ios8之后需要添加仪际,否則無法定位)
<key>NSLocationWhenInUseUsageDescription</key>
<string>YES</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>YES</string>
第三步:Appdelegate中代碼1.引入頭文件,定義全局變量
#import<CoreLocation/CoreLocation.h>
@interface AppDelegate () {
CLLocationManager * _locationManager;
}
@end
2.didFinishLaunchingWithOptions中進(jìn)行初始化(調(diào)用初始化方法),初始化方法如下:
-(void) createLocationManager{
_locationManager = [[CLLocationManager alloc] init];
if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[_locationManager requestAlwaysAuthorization];
}
if ([_locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[_locationManager setAllowsBackgroundLocationUpdates:YES];
}
_locationManager.pausesLocationUpdatesAutomatically = NO;
}
3.在applicationDidEnterBackground(進(jìn)入后臺(tái))方法中執(zhí)行啟動(dòng)定位服務(wù)
- (void)applicationDidEnterBackground:(UIApplication *)application {
[_locationManager startUpdatingLocation];
}