下面通過地圖持續定位的方式,完成App進入后台保存成功繼續運行:
第一步:開啟后台服務
第二步:在info.l <key>NSLocationAlwaysUsageDescription</key>
<string>請允許使用定位功能</string> <key>NSLocationWhenInUseUsageDescription</key> <string>請允許使用定位功能</string<key>UIBackgroundModes</key>
<key>UIBackgroundModes</key> <array> <string>fetch</string> <string>location</string> </array>
第三步:創建CLLocationManager對象,並且設置CLLocationManagerDelegate的代理
#import <CoreLocation/CLLocationManager.h>
<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager; @property (nonatomic , strong)dispatch_source_t timer;
#pragma mark - 應用進入后台執行定位 保證進程不被系統kill -(void)rjxContinuedLocationManager { //1.創建定位管理對象 _locationManager = [[CLLocationManager alloc]init]; //2.設置屬性 distanceFilter、desiredAccuracy [_locationManager setDistanceFilter:kCLDistanceFilterNone];//實時更新定位位置 [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];//定位精確度 if([_manager respondsToSelector:@selector(requestAlwaysAuthorization)]){ [_locationManager requestAlwaysAuthorization]; } //該模式是抵抗程序在后台被殺,申明不能夠被暫停 _locationManager.pausesLocationUpdatesAutomatically = NO; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) { //在后台也可定位 [_locationManager requestAlwaysAuthorization]; } if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) { _locationManager.allowsBackgroundLocationUpdates = YES; } //3.設置代理 _locationManager.delegate = self; //4.開始定位 [_locationManager startUpdatingLocation]; //5.獲取朝向 [_locationManager startUpdatingHeading]; NSLog(@"_locationManager---%@",_locationManager); // 一.請求接口獲取時間間隔,開啟定時器,紀錄當前當前的時間 [self startTimeWithInterval:5.0]; } // 開啟定時器 - (void)startTimeWithInterval:(double)time { // 紀錄當前的時間 self.startdata = [NSDate date]; NSTimeInterval period = time;//設置時間間隔 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, period * NSEC_PER_SEC, 0 * NSEC_PER_SEC); dispatch_source_set_event_handler(_timer, ^{ dispatch_async(dispatch_get_main_queue(), ^{ // 調用上傳步數的方法 NSLog(@"11111"); [self getHealthStep]; }); }); dispatch_resume(_timer); }
- (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. NSLog(@"2-------applicationDidEnterBackground"); UIApplication *app = [UIApplication sharedApplication]; __block UIBackgroundTaskIdentifier bgTask; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid){ bgTask = UIBackgroundTaskInvalid; NSLog(@"========="); } }); }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid){ bgTask = UIBackgroundTaskInvalid; NSLog(@"---------------"); } }); }); [self.locationManager startUpdatingLocation]; }
第四步:在didFinishLaunchingWithOptions方法里面調用rjxContinuedLocationManager這個方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //地圖定位 [self rjxContinuedLocationManager]; /** 應用進入后台執行定位 保證進程不被系統kill */ [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self.locationManager startUpdatingLocation]; return YES; }