Alamofire does not work when airplane mode is on

I am developing an iOS download app that uses Alamofire 4.4 and Swift 3.0.

The app gets the url and downloads the file using Alamofire. I am using a background session manager so that the app can load in the background and send a notification when finished.

For some reason, after adding this answer .failure never fires even when airplane mode is enabled. If it turns off airplane mode, the download will continue, but leave it on long enough that it won't continue to download, so you can assume it would cause a crash ...

I have this code for a download request:

    if let resumeData = resumeData {
        request = BackendAPIManager.sharedInstance.alamoFireManager.download(resumingWith: resumeData, to: destination)
    }
    else {
        request = BackendAPIManager.sharedInstance.alamoFireManager.download(extUrl, to: destination)
    }

    alamoRequest = request
        .responseData { response in

            switch response.result {
            case .success:

                //get file path of downloaded file
                let filePath = response.destinationURL?.path

                completeDownload(filePath: filePath!)

            case .failure:

                //this is what never calls on network drop out
                print("Failed")

            }

        .downloadProgress { progress in

            let progressPercent = Float(progress.fractionCompleted)

            //handle other progress stuff

        }
    }

      

.success case triggers are fine and progress returns fine. If I canceled the download using alamoRequest? .Cancel () then it raises the .failure event.

Here's my completion handler:

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.uniqudeidexample.background")
        self.alamoFireManager = Alamofire.SessionManager(configuration: configuration)
    }
}

      

And in my AppDelegate:

func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {

    BackendAPIManager.sharedInstance.backgroundCompletionHandler = completionHandler

    //do other notification stuff...
}

      

Before I added the background session it worked fine and when I activated airplane mode it failed. So am I missing something?

Hope it all makes sense, this is my first app so didn't rely on all the technical possibilities,

thank

+3


source to share





All Articles