ios 后台保活

后台保活就是在给APP添加了后台播放音乐的功能,需要在info.plist里面配置UIBackgroundModes里面加上audio,但是假如APP没有音乐播放功能,提交审核会被拒,慎用

- (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(@"app进入到后台applicationDidEnterBackground");
    //保存日志
    [BMCLogModuleLoggerManager saveAllLog];
//    [self comeToBackground];
}

-(void)comeToBackground{

    NSLog(@"%s:应用进入后台DidEnterBackground", __FUNCTION__);
    self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"123" expirationHandler:^{

       if (self.bgTask != UIBackgroundTaskInvalid) {
           //将要挂起的时候开启播放,让APP在后台保活
           [[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
           self.bgTask = UIBackgroundTaskInvalid;
           [self.mPlayer prepareToPlay];
           BOOL isucc = [self.mPlayer play];
           NSLog(@"isuccisuccisuccisucc:%d",isucc);
       }
    }];
}

//注销后台
-(void)endBack{

    NSLog(@"end=============");

}

-(AVAudioPlayer *)mPlayer{
    if (!_mPlayer) {
         /*这里是随便添加得一首音乐。真正的工程应该是添加一个尽可能小的音乐。。。0~1秒的没有声音的。循环播放就行。这个只是保证后台一直运行该软件。使得该软件一直处于活跃状态.你想操作的东西该在哪里操作就在哪里操作。
                 */
            _session = [AVAudioSession sharedInstance];
            /*打开应用会关闭别的播放器音乐*/
        //    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
        /*打开应用不影响别的播放器音乐*/
            [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
            [_session setActive:YES error:nil];
            //1.音频文件的url路径,实际开发中,用无声音乐
            NSURL *url=[[NSBundle mainBundle]URLForResource:@"test.mp3" withExtension:Nil];
            //2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)
            _mPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
            //3.缓冲
            [_mPlayer prepareToPlay];
            _mPlayer.numberOfLoops = NSUIntegerMax;
    }
    return _mPlayer;
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center removeAllDeliveredNotifications];

    NSLog(@"app进入到前台applicationWillEnterForeground");

//    if ([BYHttpClient sharedInstance].hasLogin) {
//
//        [[BYHttpClient sharedInstance] presentLoginInOtherDevice:BYLoginPageTypeFromeBack];
//    }
//    [self endBack];
//    [self.mPlayer pause];

    //重新进入到了前天再调一次激活接口
    [self enterForeground];
}

你可能感兴趣的:(ios 后台保活)