应用程序处于后台时,Alamofire后台上载不会打印进度

时间:2018-06-09 07:31:17

标签: swift file-upload background alamofire

我使用PDFAlamofire文件上传到服务器。我已经创建backgroundSessionManager以确保应用在应用程序处于后台时上传文件。当我从backgroundCompletionHandler方法AppDelegate调用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
            }
        }
    }
}

上传文件代码

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)
                }
            }

我搜索了SO和github页面但是没有找到解决方案。如果我犯了任何错误,任何人都可以指导吗?

2 个答案:

答案 0 :(得分:0)

Did you enable background services in xcode capabilities

希望这会有效,如果您还在面对,请告诉我

答案 1 :(得分:-1)

尝试在应用的后台模式功能内启用外部附件通讯功能。