Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 what it was named based on the destination I set and the original file request I made, but I would assume that there is a value I should be able to access to get the full final download path and file name in the response.

How do I access the name of the file and how do I know if it saved successfully? I can see delegate methods in the actual Alamofire code that appear to handle the delegate completion calls for the downloading process, but how do I / can I access the file details in the .response block?

like image 539
Paul Bonneville Avatar asked Oct 10 '14 19:10

Paul Bonneville


People also ask

What is the use of Alamofire?

Alamofire is a networking library written in Swift. You use it to make HTTP(S) requests on iOS, macOS and other Apple platforms. For example, to post data to a web-based REST API or to download an image from a webserver. Alamofire has a convenient API built on top of URLSession (“URL Loading System”).

What does Alamofire mean?

Alamofire is an HTTP network-based library which is used to handle the web request and response in iOS and MacOS. It is the wrapper class of URLSession and provides an interface on the top of Apple's networking stack. It simplifies the common networking tasks like preparing HTTP requests and parse the JSON object.

Is Alamofire asynchronous?

Networking in Alamofire is done asynchronously.


2 Answers

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

like image 58
Sam Avatar answered Nov 11 '22 10:11

Sam


The example on the Alamofire readme file actually has the file path in it, so I grab it with a separate variable to use elsewhere in my code. It is not very elegant and I am hoping there is a way to get that info in the response, but for now, this is getting the job done:

var fileName: String? var finalPath: NSURL?  Alamofire.download(.GET, urlToCall, { (temporaryURL, response) in      if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {              fileName = response.suggestedFilename!         finalPath = directoryURL.URLByAppendingPathComponent(fileName!)         return finalPath!     }      return temporaryURL })     .response { (request, response, data, error) in          if error != nil {             println("REQUEST: \(request)")             println("RESPONSE: \(response)")         }           if finalPath != nil {             doSomethingWithTheFile(finalPath!, fileName: fileName!)         }  } 
like image 31
Paul Bonneville Avatar answered Nov 11 '22 08:11

Paul Bonneville