iOS - 后台保活

2020-04-27  本文已影响0人  Mn_Su

一、正常延长版保活(时间3min-40min不等)

1.定义定时器属性
   @property (nonatomic,strong)NSTimer *myTimer;
   @property (nonatomic,assign)UIBackgroundTaskIdentifier backgroundTaskIdentifier;

2.代码实现
- (void)applicationDidEnterBackground:(UIApplication *)application{
    self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
      [application endBackgroundTask:self.backgroundTaskIdentifier];
      self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
    }];

//开启定时器 不断向系统请求后台任务执行的时间

     self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(makeMoreTime) userInfo:nil repeats:YES];

     [self.myTimer fire]
}

-(void)makeMoreTime {

//如果系统给的剩余时间小于60秒 就终止当前的后台任务,再重新初始化一个后台任务,重新让系统分配时间,这样一直循环下去,保持APP在后台一直处于active状态。

MSULog(@"1 --- %.2f",[UIApplication sharedApplication].backgroundTimeRemaining);

if ([UIApplication sharedApplication].backgroundTimeRemaining < 60) {

    MSULog(@"Background Time Remaining = %.02f Seconds", [UIApplication sharedApplication].backgroundTimeRemaining);

    [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];

    self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

        MSULog(@"重新");
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];

        self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;

    }];
}

二、永久版保活

第一种方式:利用后台定位功能实现,此模式只适合选择“一直允许使用定位”,但是在新系统上会出现如果用户选择“使用app时定位”,就会出现退到后台时候,手机顶部会出现蓝色定位标识,影响用户体验;如果用户拒绝使用定位,就尴尬了,除地图类app,其他app不建议此方式;

第二种方式:利用voip功能实现,此模式适合音视频类app,常规类app也可以考虑使用;voip推送和apns推送是苹果两大推送方式,和apns推送不一样的地方在于,voip推送属于强制推送,用户无法拒绝此推送模式(故在iOS13后,苹果限制voip推送使用,但不影响后台保活);实现voip推送,然后后台默认播放铃声!

  1. 选择后台模式
图片.png
  2. 应用AVFoundation和<PushKit/PushKit.h>框架、定义属性
    @property (nonatomic, strong) AVAudioPlayer *player;
    @property (nonatomic,assign)UIBackgroundTaskIdentifier backgroundTaskIdentifier;

  3. 后台铃声代码实现
    - (AVAudioPlayer *)player{
        if (!_player){
            NSURL *url=[[NSBundle mainBundle]URLForResource:@"work5.mp3" withExtension:nil];
            _player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
            [_player prepareToPlay];
    
            _player.volume = 0.00;
            //一直循环播放
            _player.numberOfLoops = -1;
            [[AVAudioSession sharedInstance] setActive:YES error:nil];
            [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
        }
        return _player;
    }

    - (void)startBgTask{
        UIApplication *application = [UIApplication sharedApplication];
        __block    UIBackgroundTaskIdentifier bgTask;
        bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
            //这里延迟的系统时间结束
            [application endBackgroundTask:bgTask];
            NSLog(@"%f",application.backgroundTimeRemaining);
        }];
    }

    - (void)applicationDidEnterBackground:(UIApplication *)application{
        [self startBgTask];
        [self.player play];
    }

     4. voip代码实现(配置voip证书省略。。。。)

      -(void)startVoIPPush{
          NSString * identifier = [[NSBundle mainBundle] bundleIdentifier];

          if ([identifier isEqualToString:@"com.eccalc.chat2u"] && [[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
                //if([[UIDevice currentDevice].systemVersion floatValue] < 10)
          {
                PKPushRegistry * pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
                pushRegistry.delegate = self;
                pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
          //}else{
  //            UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  //            center.delegate = self;
  //            [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
  //                                  completionHandler:^(BOOL granted, NSError * _Nullable error) {
  //                                      // Enable or disable features based on authorization.
  //                                  }];
  //            [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  //            }];
          }
    
  //        UIUserNotificationType types = (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert);
 //        UIUserNotificationSettings * notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
//        [[UIApplication sharedApplication]registerUserNotificationSettings:notificationSettings];
      }else{
          g_default removeObjectForKey:@"voipToken"];
      }
  }

  #pragma mark - PKPushRegistryDelegate
  -(void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(PKPushType)type{
      if ([credentials.token length] == 0) {
  //        MSULog(@"voip token NULL");
          return;
      }
      NSString * voipToken;

      if (@available(iOS 13.0, *)) {
          voipToken = [self getHexStringForData:credentials.token];
      }else {    
          voipToken = [[[[credentials.token description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
      }
      //voipToken 需要提交给服务器
      [g_default setObject:voipToken forKey:@"voipToken"];
  //    MSULog(@"voipToken:%@",voipToken);
  }

  #pragma mark - 但在iOS11.0之后,系统也提供了另外一个方法实现唤醒后的操作,此方法开启后,我项目iOS13以下机型会出现受收不到voip消息情况,所以注释了
  //- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void (^)(void))completion
  //{
  //    if (type != PKPushTypeVoIP) {
  //        return;
  //    }
  //    [self dealWithPushkitDidReceiveIncomingPushWithPayload:payload];
  //}
  #pragma mark - iOS8.0~iOS11.0是使用上面这个代理方法来实现唤醒后的操作,
  -(void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type{
      if (type != PKPushTypeVoIP) {
          return;
      }
      //此处写相关处理逻辑
      //[self dealWithPushkitDidReceiveIncomingPushWithPayload:payload];   
  }
上一篇 下一篇

猜你喜欢

热点阅读