iOS实现无限后台background的方法

前言

项目中有需求,要在后台监控某些参数,进行一些逻辑,(比如有道词典的后台复制就弹出notification进行翻译)那么就涉及到如何让app可以在后台更久的运行。

在ios7以前,后台可以用下面的的方式,去在后台存活5-10分钟,在ios8中,只能存活3分钟。

[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]
1
查询过一些资料以后,个人如果要无限的后台存活的话,可能就要涉及到后台播放音乐时最简单的办法。

首先在Required background modes加上audio,然后在applicationDidEnterBackground中进行播放音乐的操作。

正文

写了个例子代码如下:

-(void) applicationDidEnterBackground:(UIApplication *)application{
UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
 _shouldStopBg = NO; 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(){ 
    while ( TRUE ) {
        if ( _shouldStopBg ){ break; } 
        float remainTime = [[UIApplication sharedApplication] backgroundTimeRemaining]; 
        NSLog(@"###!!!BackgroundTimeRemaining: %f",remainTime);
        if ( remainTime < 20.0 ){
            NSLog(@"start play audio!"); 
            NSError *audioSessionError = nil; 
            AVAudioSession *audioSession = [AVAudioSession sharedInstance];
            if ( [audioSession setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&(audioSessionError)] )
            {
                NSLog(@"set audio session success!"); 
            }else{
                NSLog(@"set audio session fail!"); 
            } 
            NSURL *musicUrl = [[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"bgSong" ofType:@"mp3"]]; 
            self.audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:musicUrl error:nil]; 
            self.audioPlayer.numberOfLoops = 0; 
            self.audioPlayer.volume = 0;
            [self.audioPlayer play]; 
            [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil]; 
        } 
        [NSThread sleepForTimeInterval:1.0]; 
     } 
});
}


其中需要关注的是,audioplayer在arc的环境中会被release,所以需要持有,而

[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
1
需要在程序在前台的时候去在一次触发(如果在后台无法触发),所以使用音乐播放的时候的前台触发才行。

写在最后

最后,这段代码最后没敢进master,因为我们感觉审核应该无法通过。Orz,但是也记录一下这个tricky的办法吧

如果有人知道有道词典是如何实现后台复制就处理并弹出notification的,欢迎指点一下。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值