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.
    
    [self backgroundHandler];
}

- (void)backgroundHandler {
    
    NSLog(@"### -->backgroundinghandler");
    UIApplication *app = [UIApplication sharedApplication];
     UIBackgroundTaskIdentifier 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) {
            AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
            NSDictionary *parameters = @{@"email":@"849430904@qq.com",@"password":@"85252"};
            [manager POST:@"http://192.168.20.215:8080/v1/email/login" parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSLog(@"success:%@",responseObject);
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                NSLog(@"error:%@", error.userInfo);
            }];
            
            sleep(5);
        }
        
    });
}

在测试期间请求是一只发送的.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 根据苹果文档中关于后台执行的描述,任何app都有10分钟左右的后台任务执行时间。 10分钟后,app会被iOS强行...
    圈少阅读 11,198评论 3 0
  • 苹果官网地址 Background Execution (后台执行)当用于没有-启动应用,系统移到后台状态。对于很...
    helinyu阅读 12,330评论 0 9
  • IOS开发之----详解在IOS后台执行 文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来...
    dongfang阅读 5,176评论 0 7
  • 文档app在后台时会被暂停,暂停的apps会提高电池的使用寿命,并且会让系统将重要的系统资源投入到引起用户注意的前...
    zziazm阅读 10,197评论 0 5
  • 18岁的你来到陌生的城市听着陌生的方言,尽管一切都是未知但你仍对接下来的四年充满期待。 你学会了自己吃饭自己逛超市...
    小仙男呀咿呀阅读 2,350评论 0 0