ioS无限后台任务(后台长期网络请求任务)

根据苹果文档中关于后台执行的描述,任何app都有10分钟左右的后台任务执行时间。 10分钟后,app会被iOS强行挂起。

但是,有5类app允许有“无限的”后台运行时间:

1.  Audio。

2.  Location/GPS。

3.  VoIP。

4.  Newsstand。

5.  Exernal Accessory 。

你可以将任何app声明为上述5种类型以获得无限的后台运行时间,但当你提交app到App Store时,苹果会审查你的app,一旦发现你“滥用”了后台API,你的app将被拒绝。

当然,对于企业开发而言,不存在“滥用”的问题——企业app可以通过OTA部署,不经过苹果商店审查。

在企业部署中,你可以将一个app声明为VoIP,但这个程序根本和VoIP无关,我们的目的只是为了让iOS给我们无限后台执行的权限。声明过程是在app的Info.plist文件中加入以下key:

<key>UIBackgroundModes</key>

<array>

<string>voip</string>

</array>

我测试了以下代码:

- (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.
//    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
//        // Clean up any unfinished task business by marking where you
//        // stopped or ending the task outright.
//        [application endBackgroundTask:bgTask];
//        bgTask = UIBackgroundTaskInvalid;
//    }];
//    
//    // Start the long-running task and return immediately.
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        
//        // Do the work associated with the task, preferably in chunks.
//        // your code
//        NSLog(@" %f",application.backgroundTimeRemaining);
//        [application endBackgroundTask:bgTask];
//        bgTask = UIBackgroundTaskInvalid;
//    });
    
    [self backgroundHandler];
    
}

- (void)backgroundHandler {
    
    NSLog(@"### -->backgroundinghandler");
    UIApplication *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        dispatch_async(dispatch_get_main_queue(),^{
            if( bgTask != UIBackgroundTaskInvalid){
                bgTask = UIBackgroundTaskInvalid;
            }
        });
         NSLog(@"====任务完成了。。。。。。。。。。。。。。。===>");
       // [app endBackgroundTask:bgTask];
       
    }];
    
    // Start the long-running task
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        while (true) {

            [HttpTool postWithPath:@"http://192.168.20.215:8080/v1/email/login" params:@{@"email":@"849430904@qq.com",@"password":@"85252"} success:^(id JSON) {
                // code
                NSLog(@"success:%@",JSON);
            } failure:^(NSError *error) {
                //
                NSLog(@"error:%@",error.userInfo);
            }];
            sleep(5);
        }

    });
}


通过测试,我获得了“无限的”后台执行时间。我不知道你认为“无限”到底是多长时间,但在这个例子中,后台任务至少运行了55个小时以上,一直到我失去耐心停止测试。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值