I am downloading a pdf file using alamofire. This basically works, however iOS doesn't seem to overwrite the file when downloading is done several times. I get this error:
Optional (Error Domain = NSCocoaErrorDomain Code = 516 "The operation could not be completed. (Cocoa error 516.)" UserInfo = 0x1740feb80 {NSSourceFilePathErrorKey = / private / var / mobile / Containers / data / Application / B2674ABD-95F1-42AF-929914-FE29F21 /TMP/CFNetworkDownload_1b6ZK8.tmp, NSUserStringVariant = (Move), NSDestinationFilePath = / var / mobile / Containers / Data / Application / B2674ABD-95F1-42AF-9F79-FE21F2929E14 / Documents / November 2014.pdf / NSFPP private mobile / Containers / Data / Application / B2674ABD-95F1-42AF-9F79-FE21F2929E14 / tmp / CFNetworkDownload_1b6ZK8.tmp, NSUnderlyingError = 0x17405fb00 "The operation cannot be completed. The file exists"})
How can I tell alamofire to overwrite a file? My code is:
var fileName = "" var filePath = "" Alamofire.manager.download(Router.listToPdf(), destination: { (temporaryURL, response) -> (NSURL) 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 { (_, _, data, err) -> Void in }
source share