7

I am uploading PDF file to server using Alamofire. I have created backgroundSessionManager to make sure that app uploads file while app is in background. The code works perfectly as I am getting call over backgroundCompletionHandler from AppDelegate method handleEventsForBackgroundURLSession.

The progress is I am showing progress and when app goes in background the progress is stuck and .uploadProgress completion is also not calling. Due to this when user goes in background and after some time when comes back then progress is not increasing.

Shared Instance:

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

Uploading file code

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

I have search over SO and github page but din't find the solution. Any one can pls guide if I am making any mistake?

Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57

2 Answers2

0

Did you enable background services in xcode capabilities

Hope this will work, let me know if you are still facing it

iOS Lifee
  • 2,091
  • 23
  • 32
-1

Try with enabling the External accessory communication capability inside your app's Background mode Capabilities.

bra.Scene
  • 628
  • 8
  • 14