在后台继续下载
回答
做一些在后台继续的操作的一种方法是创建一个单独的线程来执行下载。在线程内部,在调用beginBackgroundTaskWithExpirationHandler:和endBackgroundTask之间包含下载操作。你不需要检查你是否在后台运行,你总是调用这两种方法。
// Tell iOS this as a background task in case we get backgrounded
UIBackgroundTaskIdentifier taskId = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:NULL];
//----------------------------------------------
// Perform your download operations here
//----------------------------------------------
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// Tell iOS that we are done with stuff that needed to keep going even if backgrounded
[[UIApplication sharedApplication] endBackgroundTask:taskId];
您可以设置ASIHttpRequest属性:[request setShouldContinueWhenAppEntersBackground:YES]; – HelloWorld
非常感谢你!如果我错了,请纠正我:总之,在后台或前台运行任务几乎没有区别。两者都将使用此代码执行。但是有两点不同:1。**如果**你的后台任务太长,那么在完成之前你可能会被终止! 2.最好'endBackgroundTask:taskId',这样可以节省额外的电池。 – Honey
对不起,我是不正确的,正如在评论中指出的,你可以延长时间限制,你必须执行一次操作/在你的应用程序进入后台之前。这里是Apple's Official Documentation
这是不正确的,你可以执行[有限长度任务](http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/ doc/uid/TP40007072-CH4-SW28)最多10分钟。 – rckoenes
@rckoenes你确实是对的,我的道歉。我会编辑我的答案。感谢您的信息:) – bennythemink
仍然不是真的,你可以调用['beginBackgroundTaskWithExpirationHandler:'](http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html# //执行Finite-Length任务之前,请先执行Apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler :)。一旦你进入后台,你不需要执行操作。您可以在将应用推送到后台之前开始执行任务。 – rckoenes
我不知道你如何处理你的数据下载。但你可以看看ASIHTTPRequest。它非常简单直接,如果将编译器标志设置为-fno-objc-arc,则适用于ARC。有了这个你只需要使用
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setShouldContinueWhenAppEntersBackground:YES]; //For iOS 4.0 and up
而且工作。
Here你可以看到ASIHTTPRequest如何工作
希望它能帮助!
ASIHTTPRequest不再积极开发......按照每条消息[here](http://allseeing-i.com/ASIHTTPRequest/)。只是说。 – zero0cool
的确如此,但它仍然像魅力一样。我还没有找到任何具有与ASIHTTPRequest一样多的功能的好选择。 – Thermometer
- 1. 继续下载被切换到后台
- 2. 在后台状态下继续操作
- 3. React-native,继续在后台下载进程
- 4. 继续在后台上传
- 5. 当应用程序进入后台时继续下载
- 6. 如何在iOS中终止应用程序后在后台继续下载
- 7. Aria2继续下载
- 8. ASIHttpRequest继续下载
- 9. 在IntegrityError后继续加载
- 10. 如何在后台继续在前台继续使用NSURLConnection开始连接?
- 11. 在后台继续执行任务
- 12. AS3 UILoader SWF在后台继续播放
- 13. iOS使AVPlayer继续在后台播放
- 14. 继续在后台运行代码ASP.NET
- 15. 继续下载与stream.addrange
- 16. iOS - AVAudioPlayer不会在后台继续播放下一首歌曲
- 17. 如何在这种情况下继续执行后台进程
- 18. setinterval在ajax加载后继续运行
- 19. 如何继续在后台运行前台服务也在android
- 20. 如何从后台继续播放
- 21. 如何在断开连接后继续下载文件?
- 22. MPMovieplayerController在停止后会继续下载(iphone)
- 23. pyinstaller程序在关闭后继续在后台运行
- 24. 继续在Java中的Ctrl + Z之后继续阅读控制台输入
- 25. 下载并在后台
- 26. AFNetworking 2.0.0在后台下载
- 27. Swift - 在后台下载NSURLSession?
- 28. 继续PostAsJsonAsyc后
- 29. 是否可以在应用程序从后台终止后继续下载文件
- 30. 按回车后继续在C中的是/否后继续
那就是做它的正式方法,THRE没有其他的方式,就像我知道 –
你应该检查这个环节http://stackoverflow.com/questions/4579810/ios-application-background-downloading – swiftBoy