ios 在后台会话中下载 AlamoFire
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30134652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AlamoFire Download in Background Session
提问by LastMove
I am using Alamofire within a new app (A Download Manager Sample based on Alamofire)
I need some clarifications about downloading files using the background session.
I need to override SessionDelegate to get it works?
Or just backgroundCompletionHandler
?
我在一个新应用程序(基于 Alamofire 的下载管理器示例)中使用 Alamofire 我需要一些关于使用后台会话下载文件的说明。我需要覆盖 SessionDelegate 才能让它工作吗?还是只是backgroundCompletionHandler
?
Typically what are the steps to handle downloads in background using Alamofire? And how can I handle the case where my app is relauch, with downloads in flux.
通常使用 Alamofire 在后台处理下载的步骤是什么?以及如何处理我的应用程序重新发布且下载量不断变化的情况。
采纳答案by József Vesza
Update
更新
Based on this amazing tutorial, I have put together an example project available on GitHub. It has an example for background session management.
基于这个惊人的教程,我在GitHub 上整理了一个示例项目。它有一个后台会话管理的例子。
According to Apple's URL Loading System Programming Guide:
根据 Apple 的URL 加载系统编程指南:
In both iOS and OS X, when the user relaunches your app, your app should immediately create background configuration objects with the same identifiers as any sessions that had outstanding tasks when your app was last running, then create a session for each of those configuration objects. These new sessions are similarly automatically reassociated with ongoing background activity.
在 iOS 和 OS X 中,当用户重新启动您的应用程序时,您的应用程序应立即创建与上次运行时具有未完成任务的任何会话具有相同标识符的后台配置对象,然后为每个配置对象创建一个会话. 这些新会话类似地自动与正在进行的后台活动重新关联。
So apparently by using the appropriate background session configuration instances, your downloads will never be "in flux".
因此,显然通过使用适当的后台会话配置实例,您的下载将永远不会“不断变化”。
I have also found this answerreally helpful.
我也发现这个答案真的很有帮助。
Original answer
原答案
From Alamofire's GitHub page:
来自 Alamofire 的GitHub 页面:
Applications can create managers for background and ephemeral sessions, as well as new managers that customize the default session configuration, such as for default headers (HTTPAdditionalHeaders) or timeout interval (timeoutIntervalForRequest).
应用程序可以为后台和临时会话创建管理器,以及自定义默认会话配置的新管理器,例如默认标头 (HTTPAdditionalHeaders) 或超时间隔 (timeoutIntervalForRequest)。
By default, top level methods use a shared Manager
instance with default session configuration. You can however create a manager with background session configuration like so:
默认情况下,顶级方法使用Manager
具有默认会话配置的共享实例。但是,您可以创建一个具有后台会话配置的管理器,如下所示:
let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("com.example.app.background")
let manager = Alamofire.Manager(configuration: configuration)
You can then make requests using this Manager
instance.
然后,您可以使用此Manager
实例发出请求。
manager.startRequestsImmediately = true
let request = NSURLRequest(URL: NSURL(string: "your.url.here")!)
manager.request(request)
By looking at its implementation, it also has a property called backgroundCompletionHandler
, so you can add a completion block:
通过查看它的实现,它还有一个名为 的属性backgroundCompletionHandler
,因此您可以添加一个完成块:
manager.backgroundCompletionHandler = {
// do something when the request has finished
}
回答by Luca Torella
EDIT
编辑
With Alamofire 5 this is no longer possible, see the release notes:
对于 Alamofire 5,这不再可能,请参阅发行说明:
Using a
URLSessionConfiguration
with a background identifier is not possible any more. We're explicitly ensuring Alamofire isn't used with background sessions, in order to prevent ongoing issues around support and surprise on the part of the user.
URLSessionConfiguration
不再可能将 a与背景标识符一起使用。我们明确确保 Alamofire 不与后台会话一起使用,以防止围绕支持和用户意外的持续问题。
Old answer, still valid if you use Alamofire 4
旧答案,如果您使用 Alamofire 4 仍然有效
It's actually very easy with Alamofire:
使用 Alamofire 实际上很容易:
1) your Alamofire.Manager should be configured with a background session identifier:
1) 你的 Alamofire.Manager 应该配置一个后台会话标识符:
class NetworkManager {
...
private lazy var backgroundManager: Alamofire.SessionManager = {
let bundleIdentifier = ...
return Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: bundleIdentifier + ".background"))
}()
...
}
2) in the App Delegate implement application(_:handleEventsForBackgroundURLSession:completionHandler:
and pass the completion handler to Alamofire.SessionManager.backgroundCompletionHandler
.
2) 在 App Delegate 中实现application(_:handleEventsForBackgroundURLSession:completionHandler:
并将完成处理程序传递给Alamofire.SessionManager.backgroundCompletionHandler
.
In my case the app delegate method looks like
在我的情况下,应用程序委托方法看起来像
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
NetworkManager.default.backgroundCompletionHandler = completionHandler
}
and my network manager has a computed property like this to set the Manager property:
我的网络管理器有一个这样的计算属性来设置管理器属性:
var backgroundCompletionHandler: (() -> Void)? {
get {
return backgroundManager.backgroundCompletionHandler
}
set {
backgroundManager.backgroundCompletionHandler = newValue
}
}
回答by Naloiko Eugene
I was searching for the solution quite long. Until read the article mentioned above. The issue for me was - I had to enable External accessory communication
我一直在寻找解决方案。直到阅读了上面提到的文章。我的问题是 - 我必须启用外部附件通信
Everything else was done as described above. AppDelegate:
其他一切都按上述方式完成。应用委托:
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
BackendAPIManager.sharedInstance.backgroundCompletionHandler = completionHandler
}
Singleton:
单身人士:
import Alamofire
class BackendAPIManager: NSObject {
static let sharedInstance = BackendAPIManager()
var alamoFireManager : Alamofire.SessionManager!
var backgroundCompletionHandler: (() -> Void)? {
get {
return alamoFireManager?.backgroundCompletionHandler
}
set {
alamoFireManager?.backgroundCompletionHandler = newValue
}
}
fileprivate override init()
{
let configuration = URLSessionConfiguration.background(withIdentifier: "com.url.background")
configuration.timeoutIntervalForRequest = 200 // seconds
configuration.timeoutIntervalForResource = 200
self.alamoFireManager = Alamofire.SessionManager(configuration: configuration)
}
}
And the calls are done in the following way:
调用是通过以下方式完成的:
BackendAPIManager.sharedInstance.alamoFireManager.upload(multipartFormData: { (multipartFormData) in ...