Alamofire.download() method: Where is the file and did it save successfully?

后端 未结 5 725
无人及你
无人及你 2021-02-02 11:16

The example for using Alamofire.download() works just fine, but there isn\'t any detail in how to access the resulting downloaded file. I can figure out where the file is and wh

5条回答
  •  广开言路
    2021-02-02 11:40

    Swift 2.1 and a little simpler:

    var localPath: NSURL?
    Alamofire.download(.GET,
        "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v",
        destination: { (temporaryURL, response) in
        let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
        let pathComponent = response.suggestedFilename
                
        localPath = directoryURL.URLByAppendingPathComponent(pathComponent!)
            return localPath!
        })
        .response { (request, response, _, error) in
            print(response)
            print("Downloaded file to \(localPath!)")
        }
    )
    

    Still hard to understand why they are using closures to set the destination path...

提交回复
热议问题