iOS让App后台运行方法小结
2017-09-16 15:26
525 查看
一般App进入后台之后,超过了后台运行时间,便进入了挂起状态,无法执行代码,但是内存并没有清除。
主要用到2个方法:
beginBackgroundTaskWithExpirationHandler://申请后台,该方法只有在App处于激活时调用才有效。 endBackgroundTask://注销后台
后台执行3分钟方法:
无限后台的方法,慎用!因为这个需要申请后台播放音频的权限。如果你的应用不是相关应用,AppStore审核可能不会通过。
好了,看方法!
先在info.plist文件里添加如此一条记录:
主要用到2个方法:
beginBackgroundTaskWithExpirationHandler://申请后台,该方法只有在App处于激活时调用才有效。 endBackgroundTask://注销后台
后台执行3分钟方法:
#import "AppDelegate.h" @interface AppDelegate (){ NSInteger count; } @property(strong, nonatomic)NSTimer *mTimer; @property(assign, nonatomic)UIBackgroundTaskIdentifier backIden; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. count=0; return YES; } - (void)applicationDidEnterBackground:(UIApplication *)application { _mTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countAction) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_mTimer forMode:NSRunLoopCommonModes]; [self beginTask]; } - (void)applicationWillEnterForeground:(UIApplication *)application { NSLog(@"进入前台"); [self endBack]; } //计时 -(void)countAction{ NSLog(@"%li",count++); } //申请后台 -(void)beginTask { NSLog(@"begin============="); _backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ //在时间到之前会进入这个block,一般是iOS7及以上是3分钟。按照规范,在这里要手动结束后台,你不写也是会结束的(据说会crash) NSLog(@"将要挂起============="); [self endBack]; }]; } //注销后台 -(void)endBack { NSLog(@"end============="); [[UIApplication sharedApplication] endBackgroundTask:_backIden]; _backIden = UIBackgroundTaskInvalid; } @endAppDelegate.m文件 #import "AppDelegate.h" @interface AppDelegate (){ NSInteger count; } @property(strong, nonatomic)NSTimer *mTimer; @property(assign, nonatomic)UIBackgroundTaskIdentifier backIden; @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. count=0; return YES; } - (void)applicationDidEnterBackground:(UIApplication *)application { _mTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countAction) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_mTimer forMode:NSRunLoopCommonModes]; [self beginTask]; } - (void)applicationWillEnterForeground:(UIApplication *)application { NSLog(@"进入前台"); [self endBack]; } //计时 -(void)countAction{ NSLog(@"%li",count++); } //申请后台 -(void)beginTask { NSLog(@"begin============="); _backIden = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ NSLog(@"将要挂起============="); [self endBack]; }]; } //注销后台 -(void)endBack { NSLog(@"end============="); [[UIApplication sharedApplication] endBackgroundTask:_backIden]; _backIden = UIBackgroundTaskInvalid; } @end ViewController.m文件 #import "ViewController.h" #import @interface ViewController () @property(strong, nonatomic)AVAudioPlayer *mPlayer; @property(assign, nonatomic)CGFloat mCount; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _mCount = 0; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(countTime) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; } -(void)countTime{ _mCount+=10; NSLog(@"%f",_mCount); if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 60.) {//当剩余时间小于60时,开如播放音乐,并用这个假前台状态再次申请后台 NSLog(@"播放%@",[NSThread currentThread]); [self playMusic]; //申请后台 [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ NSLog(@"我要挂起了"); }]; } } -(void)playMusic{ //1.音频文件的url路径,实际开发中,用无声音乐 NSURL *url=[[NSBundle mainBundle]URLForResource:@"欢沁.mp3" withExtension:Nil]; //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url) _mPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil]; //3.缓冲 [_mPlayer prepareToPlay]; //4.播放 [_mPlayer play]; } @end
无限后台的方法,慎用!因为这个需要申请后台播放音频的权限。如果你的应用不是相关应用,AppStore审核可能不会通过。
好了,看方法!
先在info.plist文件里添加如此一条记录:
相关文章推荐
- 针对iOS的系统App后台运行的方法
- iOS开发小结 - 让你的APP后台运行
- iOS开发小结 - 让你的APP后台运行
- iOS 前后台切换(APP运行期间生命周期)
- iOS让App后台运行
- 三十,iOS当使用运行时替换了NSArrary的方法之后,弹出键盘进入后台然后在进入前台会出现崩溃的问题
- iOS 给测试人员测试手机APP的四种方法:真机运行(略),打ipa包,(testFlighe)邮件,蒲公英(一)打ipa包
- iOS App运行在后台实现定位以及播放音频功能
- iOS-OC-APP后台持续运行
- iOS-APP后台运行代码
- iOS app 后台运行利用本地推送处理蓝牙设备通讯指令
- iOS App进入后台获取更多的运行时间
- iOS App 后台运行
- iOS10下app运行中,但是处于后台,点击通知问题
- iOS App 后台运行调研
- iOS 让 APP 在后台长时间运行
- IOS App 后台运行
- iOS App进入后台获取更多的运行时间
- iOS 怎么自定制推送声音呢?(APP运行时和APP进入后台时)
- IOS App 后台运行