AlamoFire Download in Background Session

前端 未结 3 1217
南方客
南方客 2020-12-02 10:12

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

3条回答
  •  既然无缘
    2020-12-02 10:29

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

提交回复
热议问题