iOS 后台持续定位,避免被系统回收

iOS 后台持续定位,避免被系统回收// Override point for customization after application launch.

大家好,欢迎来到IT知识分享网。

开启后台服务,如下图

iOS 后台持续定位,避免被系统回收

在info.list文件里面需要开启持续定位服务

iOS 后台持续定位,避免被系统回收

在AppDelegate里面添加如下代码

  1. 创建CLLocationManager对象,并且设置CLLocationManagerDelegate的代理
  2. 在didFinishLaunchingWithOptions方法里面调用rjxContinuedLocationManager这个方法
#import "AppDelegate.h" #import <MapKit/MapKit.h> @interface AppDelegate()<CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *manager; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //设置后台模式和锁屏模式下依然能够播放 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil]; [self rjxContinuedLocationManager]; [GeneratedPluginRegistrant registerWithRegistry:self]; // Override point for customization after application launch. return [super application:application didFinishLaunchingWithOptions:launchOptions]; } // ios 8.x or older -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{ return [FlutterAlipayPlugin handleOpenURL:url]; } // ios 9.0+ -(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{ return [FlutterAlipayPlugin handleOpenURL:url]; } /** 应用进入后台执行定位 保证进程不被系统kill */ -(void)rjxContinuedLocationManager { //1.创建定位管理对象 _manager = [[CLLocationManager alloc]init]; //2.设置属性 distanceFilter、desiredAccuracy [_manager setDistanceFilter:kCLDistanceFilterNone];//实时更新定位位置 [_manager setDesiredAccuracy:kCLLocationAccuracyBest];//定位精确度 if([_manager respondsToSelector:@selector(requestAlwaysAuthorization)]){ [_manager requestAlwaysAuthorization]; } //该模式是抵抗程序在后台被杀,申明不能够被暂停 _manager.pausesLocationUpdatesAutomatically = NO; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) { //在后台也可定位 [_manager requestAlwaysAuthorization]; } if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) { _manager.allowsBackgroundLocationUpdates = YES; } //3.设置代理 _manager.delegate = self; //4.开始定位 [_manager startUpdatingLocation]; //5.获取朝向 [_manager startUpdatingHeading]; } // 当你的程序将要被挂起,会调用改方法 - (void)applicationWillResignActive:(UIApplication *)application { /** 应用进入后台执行定位 保证进程不被系统kill */ [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self.manager startUpdatingLocation]; } /** 应用进入后台执行定位 保证进程不被系统kill */ - (void)applicationDidEnterBackground:(UIApplication *)application { UIApplication *app = [UIApplication sharedApplication]; __block UIBackgroundTaskIdentifier bgTask; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid){ bgTask = UIBackgroundTaskInvalid; } }); }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_main_queue(), ^{ if (bgTask != UIBackgroundTaskInvalid){ bgTask = UIBackgroundTaskInvalid; } }); }); [self.manager startUpdatingLocation]; } @end 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/54987.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信