iosswiftswift3alamofirealamofireimage

Download File Using Alamofire 4.0 (Swift 3)


In older version of Alamofire. This is how I download file

    let destinationPath = Alamofire.Request.suggestedDownloadDestination( directory: .documentDirectory, domain: .userDomainMask);

    Alamofire.download(.GET, urlString, destination: destinationPath)
        .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
//                print(totalBytesRead)
        }
        .response { request, response, _, error in

            let downloadedFilePath = destinationPath(URL(string: "")!, response!);

            NSUserDefaultsHelper.saveURL(downloadedFilePath, key: urlString);

            completion(downloadedFilePath, true);
    }

But now in the new version, my code is completely unusable and there is no similar function in the Alamofire library.

Any ideas please?


Solution

  • I used to use this statements:

    let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
    
    Alamofire.download(
        url,
        method: .get,
        parameters: parameters,
        encoding: JSONEncoding.default,
        headers: nil,
        to: destination).downloadProgress(closure: { (progress) in
            //progress closure
        }).response(completionHandler: { (DefaultDownloadResponse) in
            //here you able to access the DefaultDownloadResponse
            //result closure
        })
    

    For more details read more in Alamofire docs about Migration to 4.0: