介绍
这篇文章主要介绍iOS如何实现后台长时间运行,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
应用申请后台运行的方式有几种:
播放音乐
定位
报摊下载
获取等;
这里主要说下后台播放无声音乐(其实是不播放),采用哪种方式,对应勾选上图;我的项目中有音频播放需求,如果没有,那就找一个播放音频的理由,或者用其他方式实现。
<强>实现
强>
这里采用了两个单例:电话监控(XKTelManager),后台运行(XKBGRunManager),电话监控可以忽略,视情况而用,采用单例是为了方便管理;
<强> XKTelManager。h 强>
# import & lt;基?Foundation.h> @interface XKTelManager : NSObject///是否在后台运行 @property (原子,分配),BOOL inBackgroundRun; +,(XKTelManager *) sharedManager;/* * ,来电监听 ,*/安康;(void) startMonitor; @end
<强> XKTelManager。m 强>
# import “XKTelManager.h" # import “XKBGRunManager.h" # import & lt; CoreTelephony/CTCallCenter.h> # import & lt; CoreTelephony/CTCall.h> static XKTelManager * _sharedManger; @interface XKTelManager () @property (原子,,强烈),CTCallCenter * callCenter; @end @implementation XKTelManager +,(XKTelManager *) sharedManager { static 才能;dispatch_once_t  onceTelSingle; dispatch_once才能(及onceTelSingle, ^ { ,,,if (! _sharedManger), { ,,,,,_sharedManger =, [[XKTelManager alloc] init]; ,,,} ,,}); return 才能;_sharedManger; } 安康;(instancetype) init { self 才能=,(super init); if 才能;(自我),{ ,,,_inBackgroundRun =,没有; ,,} return 才能;自我; } # pragma mark - * * * * * * * * *,监听电话相关 安康;(空白)startMonitor { __weak 才能typeof(自我),weakSelf =,自我; _callCenter 才能=,[[CTCallCenter alloc], init); 时间=_callCenter.callEventHandler 才能;^ (CTCall *,调用),{ ,,,///如果已经进入后台了,不做任何操作 ,,,if (weakSelf.inBackgroundRun), { ,,,,,返回; ,,,} ,,,///应用程序未进入后台 ,,,if ([call.callState isEqualToString: CTCallStateDisconnected]) { ,,,,,NSLog (@"电话,——安康;断开连接“); ,,,,,[[XKBGRunManager sharedManager], stopBGRun); ,,,} ,,,else if ([call.callState isEqualToString: CTCallStateConnected]) { ,,,,,NSLog (@"电话,——安康;接通“); ,,,} ,,,else if ([call.callState isEqualToString: CTCallStateIncoming]) { ,,,,,NSLog (@"电话,——安康;待接通“); ,,,,,[[XKBGRunManager sharedManager], startBGRun); ,,,} ,,,else if ([call.callState isEqualToString: CTCallStateDialing]) { ,,,,,NSLog (@"电话,——安康;拨号中“); ,,,,,[[XKBGRunManager sharedManager], startBGRun); ,,,} ,,,else { ,,,,,NSLog (@"电话,——安康;无操作“); ,,,} ,,,, ,,}; } @end
<强> XKBGRunManager。h 强>
# import & lt;基?Foundation.h> @interface XKBGRunManager : NSObject +,(XKBGRunManager *) sharedManager;/* * ,开启后台运行 ,*/安康;(void) startBGRun;/* * ,关闭后台运行 ,*/安康;(void) stopBGRun; @end
<强> XKBGRunManager。m 强>
# import “XKBGRunManager.h"///循环时间 时间=static NSInteger  _circulaDuration 60; static XKBGRunManager * _sharedManger; @interface XKBGRunManager () @property (原子,分配),UIBackgroundTaskIdentifier 任务;///后台播放 @property (原子、强),AVAudioPlayer * playerBack; @property (原子,,强烈),NSTimer * timerAD;///用来打印测试 @property (原子,,强烈),NSTimer * timerLog; @property (原子,分配),NSInteger 计算; @end @implementation XKBGRunManager { CFRunLoopRef 才能;_runloopRef; dispatch_queue_t 才能;_queue; } +,(XKBGRunManager *) sharedManager { static 才能;dispatch_once_t  onceRunSingle; dispatch_once才能(及onceRunSingle, ^ { ,,,if (! _sharedManger), { ,,,,,_sharedManger =, [[XKBGRunManager alloc] init]; ,,,} ,,}); null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nulliOS如何实现后台长时间运行