回答
如果使用NSURLConnection
,你可以使用一个UIBackgroundTaskIdentifier
,以保持您的连接活着即使该应用程序发送到后台:
声明你的后台任务标识符,这样你就可以在任何地方访问它的类实现,例如在您.m文件的顶部:
@implementation MyClass {
UIBackgroundTaskIdentifier backgroundTaskID;
}
创建连接:
NSURLRequest *request = [NSURLRequest requestWithURL: aURL];
NSURLConnection *uploadConnection = [NSURLConnection connectionWithRequest:request delegate:self];
backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
// Cancel the connection in case of expiration
[uploadConnection cancel];
}];
结束你的后台任务在这两种方法NSURLConnectionDelegate
连接结束后/失败:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Handle a fail
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Handle a success
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
}
我们是否需要为此设置任何UIBackgroundModes? –
不是100%可靠的答案,但我很肯定你不需要。 – rdurand
我也这么认为,但我只是为一个客户做了这个,他说如果他去背景上传仍然失败..反正TNN –
- 1. iOS聊天应用程序如何在后台继续运行?
- 2. 继续在后台上传
- 3. 如何在iOS中终止应用程序后在后台继续下载
- 4. iOS应用程序在App Store更新后是否继续在后台运行?
- 5. 如何使广播应用程序继续在iOS应用程序的后台播放?
- 6. 如何在后台继续在iOS上运行位置更新
- 7. 为什么我的iPhone应用程序在后台继续?
- 8. 如何在iOS中创建后台线程,在应用程序已被隐藏后继续下一步
- 9. Android:继续在后台运行应用程序
- 10. iOS使AVPlayer继续在后台播放
- 11. 如何让应用程序在后台运行?继续收集数据?
- 12. 需要youtube音频继续播放时,iOS应用程序进入后台
- 13. 如何在后台继续在前台继续使用NSURLConnection开始连接?
- 14. 如何在后台继续运行python程序
- 15. MPMusicPlayerController在后台暂停并在应用程序进入前台时继续
- 16. 当应用程序转到后台时继续运行NSOperationQueue
- 17. 当应用程序进入后台时继续下载
- 18. 如何让应用程序在退出后继续运行?
- 19. 在后台运行iOS应用程序
- 20. IOS CancelNotificaion当应用程序在后台
- 21. iOS应用程序在后台
- 22. 如何在终止ssh后继续在Raspberry Pi的后台运行Python程序
- 23. 如何让流星手机应用继续在后台运行?
- 24. pyinstaller程序在关闭后继续在后台运行
- 25. 如何连续运行应用程序而不挂在后台
- 26. 在后台继续下载
- 27. 我将应用程序推入后台后,如何继续接收通过NSLog()发送的控制台信息?
- 28. 如何将ios应用程序从后台前台
- 29. 如何在iOS应用程序处于后台时调用Method?
- 30. 即使应用程序退出后,如何使用UIAutomation仪器继续测试iOS应用程序?
短语“驳回”的意思是_terminated_或去_background_? – holex
@holex去背景。 – user3374800
@ user3374800:我的答案有什么好运? – rdurand