Soh*_*mon 5 background file-upload swift alamofire
我正在使用将PDF文件上传到服务器Alamofire。我创建的backgroundSessionManager目的是确保应用程序在后台运行时上传文件。当我backgroundCompletionHandler从AppDelegatemethod 调用时,代码可以正常工作handleEventsForBackgroundURLSession。
进度是我正在显示进度,当应用程序进入后台时,进度卡住了,.uploadProgress完成也没有调用。因此,当用户进入后台并且一段时间后返回时,进度不会增加。
共享实例:
class Networking {
static let sharedInstance = Networking()
public var backgroundSessionManager: Alamofire.SessionManager // your web services you intend to keep running when the system backgrounds your app will use this
private init() {
self.backgroundSessionManager = Alamofire.SessionManager(configuration: URLSessionConfiguration.background(withIdentifier: "com.dw.myapp"))
var backgroundCompletionHandler: (() -> Void)? {
get {
return backgroundSessionManager.backgroundCompletionHandler
}
set {
backgroundSessionManager.backgroundCompletionHandler = newValue
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
上载文件代码
Networking.sharedInstance.backgroundSessionManager.upload(multipartFormData: { (multipartData) in
}, usingThreshold: UInt64.init(), to: "\(url)", method: .post, headers: headers) { (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
//Does not call while app is in background
Helper.dispatchMain {
print(progress)
print("Upload Time ", Date())
}
})
upload.responseJSON { response in
print("Response Time ", Date())
print("Response Time ", Date())
}
case .failure(let error):
completion!(["message" : error],false)
}
}
Run Code Online (Sandbox Code Playgroud)
我已经搜索过SO和github页面,但找不到解决方案。如果我有任何错误,谁可以指导?