iOS-如何保证app进入后台后持续运行
下面通过地图持续定位的方式,完成App进入后台保存成功继续运行:
第一步:开启后台服务
第二步:在info.l <key>NSLocationAlwaysUsageDescription</key>
<string>请允许使用定位功能</string> <key>NSLocationWhenInUseUsageDescription</key> <string>请允许使用定位功能</string<key>UIBackgroundModes</key>
1 2 3 4 5 | < key >UIBackgroundModes</ key > < array > < string >fetch</ string > < string >location</ string > </ array > |
第三步:创建CLLocationManager对象,并且设置CLLocationManagerDelegate的代理
1 | #import <CoreLocation/CLLocationManager.h> |
1 | <CLLocationManagerDelegate> |
1 2 | @property ( nonatomic , strong) CLLocationManager *locationManager; @property ( nonatomic , strong)dispatch_source_t timer; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | - ( 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这个方法
1 2 3 4 5 6 7 8 9 10 11 | - ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions { //地图定位 [ self rjxContinuedLocationManager]; /** 应用进入后台执行定位 保证进程不被系统kill */ [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [ self .locationManager startUpdatingLocation]; return YES ; } |
分类:
iOS中的笔记
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了