在info.plist中配置app
<key>NSLocationAlwaysUsageDescription</key> <string>始终获取定位服务</string> <key>UIBackgroundModes</key> <array> <string>fetch</string> <string>location</string> <string>remote-notification</string> </array>
引用fetch
#import <CoreLocation/CoreLocation.h>atom
定义一个code
@property(nonatomic,strong)CLLocationManager *locMgr;对象
实例化locMgr对象后,要设置一下两个才能持续后台定位pausesLocationUpdatesAutomatically和allowsBackgroundLocationUpdatesip
只设置其中一个是不能实现后台持续定位的,网上的资料就是少了这两个设置,其余的设置就和日常普通定位同样.rem
self.locMgr.pausesLocationUpdatesAutomatically=NO; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) { //[_locationManager requestWhenInUseAuthorization]; [self.locMgr requestAlwaysAuthorization]; } if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) { self.locMgr.allowsBackgroundLocationUpdates = YES; }
这样设置后,只要定位服务是开启的,该app就能在后台一直跑.服务关闭,app到后台就会中止运行string