一、首先。在Info.plist里面添加
Key:Required background modes
Value:App registers for location updates
二、初始化
#define RUNTIME 60 * 60//当然是控制在一小时以内
- ( void )viewDidLoad
{
[ super viewDidLoad ];
[ self initData ];
// 响应后台
[[ NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (applicationDidEnterBackground:) name : UIApplicationDidEnterBackgroundNotification object : nil ];
}
// 初始化数据
-( void )initData{
backgroundUpdateInterval = RUNTIME ; // 设置计时器时间
self . _saveLocations = [[ NSMutableArray alloc ] init ];
self . _locationManager = [[ CLLocationManager alloc ] init ];
self . _locationManager . desiredAccuracy = kCLLocationAccuracyBest ;
self . _locationManager . delegate = self ;
[ self . _locationManager startUpdatingLocation ];
}
三、更新定位
- ( void )locationManager:( CLLocationManager *)manager
didUpdateToLocation:( CLLocation *)newLocation
fromLocation:( CLLocation *)oldLocation
{
// 在地图上加大头针
MKPointAnnotation *annotation = [[ MKPointAnnotation alloc ] init ];
annotation. coordinate = newLocation. coordinate ;
[ self . _mapView addAnnotation :annotation]; //
[ self . _saveLocations addObject :annotation];
if ( UIApplication . sharedApplication . applicationState == UIApplicationStateActive )
{
if ( backgroundTask != UIBackgroundTaskInvalid ) // 如果后台没有关闭,结束
{
[[ UIApplication sharedApplication ] endBackgroundTask : backgroundTask ];
backgroundTask = UIBackgroundTaskInvalid ;
}
// 显示所有的大头针
for ( MKPointAnnotation *annotation in self . _saveLocations )
{
CLLocationCoordinate2D coordinate = annotation. coordinate ;
MKCoordinateRegion region= MKCoordinateRegionMakeWithDistance (coordinate, storedLatitudeDelta , storedLongitudeDelta );
MKCoordinateRegion adjustedRegion = [ _mapView regionThatFits :region];
[ _mapView setRegion :adjustedRegion animated : NO ];
}
}
else
{
NSLog ( @"applicationD in Background,newLocation:%@" , newLocation);
}
}
四、后台加入计时器
// 用定时器控制后台运行定位时间
-( void )applicationDidEnterBackground:( NSNotificationCenter *)notication{
UIApplication * app = [ UIApplication sharedApplication ];
backgroundTask = [app beginBackgroundTaskWithExpirationHandler :^{
NSLog ( @"applicationD in Background" );
}];
// 加入定时器,用来控制后台运行时间
self . _updateTimer = [ NSTimer scheduledTimerWithTimeInterval : backgroundUpdateInterval
target : self
selector : @selector (stopUpdate)
userInfo : nil
repeats : YES ];
[[ NSRunLoop currentRunLoop ] addTimer : self . _updateTimer forMode : NSRunLoopCommonModes ];
}
五、计时时间到,停止后台运行与定位
-( void )stopUpdate{
[ self . _locationManager stopUpdatingLocation ];
[ self . _updateTimer invalidate ];
self . _updateTimer = nil ;
if ( backgroundTask != UIBackgroundTaskInvalid )
{
[[ UIApplication sharedApplication ] endBackgroundTask : backgroundTask ];
backgroundTask = UIBackgroundTaskInvalid ;
}
}
六、用模拟器测试方法
用模拟器测试的时候选择:调试-位置-后三者都行
Value:App registers for location updates
二、初始化
#define RUNTIME 60 * 60//当然是控制在一小时以内
- ( void )viewDidLoad
{
[ super viewDidLoad ];
[ self initData ];
// 响应后台
[[ NSNotificationCenter defaultCenter ] addObserver : self selector : @selector (applicationDidEnterBackground:) name : UIApplicationDidEnterBackgroundNotification object : nil ];
}
// 初始化数据
-( void )initData{
backgroundUpdateInterval = RUNTIME ; // 设置计时器时间
self . _saveLocations = [[ NSMutableArray alloc ] init ];
self . _locationManager = [[ CLLocationManager alloc ] init ];
self . _locationManager . desiredAccuracy = kCLLocationAccuracyBest ;
self . _locationManager . delegate = self ;
[ self . _locationManager startUpdatingLocation ];
}
三、更新定位
- ( void )locationManager:( CLLocationManager *)manager
didUpdateToLocation:( CLLocation *)newLocation
fromLocation:( CLLocation *)oldLocation
{
// 在地图上加大头针
MKPointAnnotation *annotation = [[ MKPointAnnotation alloc ] init ];
annotation. coordinate = newLocation. coordinate ;
[ self . _mapView addAnnotation :annotation]; //
[ self . _saveLocations addObject :annotation];
if ( UIApplication . sharedApplication . applicationState == UIApplicationStateActive )
{
if ( backgroundTask != UIBackgroundTaskInvalid ) // 如果后台没有关闭,结束
{
[[ UIApplication sharedApplication ] endBackgroundTask : backgroundTask ];
backgroundTask = UIBackgroundTaskInvalid ;
}
// 显示所有的大头针
for ( MKPointAnnotation *annotation in self . _saveLocations )
{
CLLocationCoordinate2D coordinate = annotation. coordinate ;
MKCoordinateRegion region= MKCoordinateRegionMakeWithDistance (coordinate, storedLatitudeDelta , storedLongitudeDelta );
MKCoordinateRegion adjustedRegion = [ _mapView regionThatFits :region];
[ _mapView setRegion :adjustedRegion animated : NO ];
}
}
else
{
NSLog ( @"applicationD in Background,newLocation:%@" , newLocation);
}
}
四、后台加入计时器
// 用定时器控制后台运行定位时间
-( void )applicationDidEnterBackground:( NSNotificationCenter *)notication{
UIApplication * app = [ UIApplication sharedApplication ];
backgroundTask = [app beginBackgroundTaskWithExpirationHandler :^{
NSLog ( @"applicationD in Background" );
}];
// 加入定时器,用来控制后台运行时间
self . _updateTimer = [ NSTimer scheduledTimerWithTimeInterval : backgroundUpdateInterval
target : self
selector : @selector (stopUpdate)
userInfo : nil
repeats : YES ];
[[ NSRunLoop currentRunLoop ] addTimer : self . _updateTimer forMode : NSRunLoopCommonModes ];
}
五、计时时间到,停止后台运行与定位
-( void )stopUpdate{
[ self . _locationManager stopUpdatingLocation ];
[ self . _updateTimer invalidate ];
self . _updateTimer = nil ;
if ( backgroundTask != UIBackgroundTaskInvalid )
{
[[ UIApplication sharedApplication ] endBackgroundTask : backgroundTask ];
backgroundTask = UIBackgroundTaskInvalid ;
}
}
六、用模拟器测试方法
用模拟器测试的时候选择:调试-位置-后三者都行
![](http://cdn.cocimg.com/bbs/attachment/Fid_19/19_2069_002a3545dbb1ff7.png)