即使应用程序处于后台模式(最小化应用程序),我也有发送和接收数据的代码:
MyViewController.m
-(void)viewDidAppear:(BOOL)animated{
[self doUpdateEvenAppMinimized];
}
- (void) doUpdate{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self beginBackgroundUpdateTask];
[self sendFilesToServer];//Inside this method have a sleep that call every 5 minutes
//The code used in sendFilesToServer is the same in this website https://dcraziee.wordpress.com/2013/05/29/how-to-upload-file-on-server-in-objective-c/
//[self endBackgroundUpdateTask];//This method is forever...so I not need to call this line
});
}
- (void)beginBackgroundUpdateTask{
self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
- (void) endBackgroundUpdateTask{
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}
文档说最长时间是 10 分钟,要删除它,我使用实现长时间运行的任务 的概念,为此我选择我的项目 > 功能 > 后台模式(打开) > 外部附件通信(选中)。
通过这些步骤,我的申请将免除 10 分钟?
最佳答案
试图规避规则在后台运行听起来是错误的做法。考虑使用 NSURLSession
用于与应用的生命周期无关的长时间运行的网络操作。
关于ios - 在后台模式下运行网络连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28541224/