Path to downloaded file with Alamofire4 (Swift3) - ios

I download a zip file with Alamofire4 and unzip it with SSZipArchive but the unzip does not work. I am not sure if the path of the downloaded file is good with Alamofire.
Here is the code:
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
return (documentsURL, [.removePreviousFile])
}
Alamofire.download(urlString, method: .get, parameters: parameters, encoding: JSONEncoding.default, to: destination)
.response{ response in
if response.error == nil {
let filename = response.response?.suggestedFilename
var folderDestination=response.destinationURL?.path
folderDestination=folderDestination?.appending("/\(nameCategory)")
archiveToUnzip=(folderDestination?.appending("/\(filename!)"))!
//unzip
let successUnZip=SSZipArchive.unzipFile(atPath: archiveToUnzip, toDestination:folderDestination!)
if !successUnZip {
SpeedLog.print("Problem unzip")
}
}
}
It displays "Problem unzip", so am I wrong in the path to the zip file?

Before unzipping try to check that all paths were correct:
guard let zipPath = (folderDestination?.appending("/\(filename!)"))! else {
print("Error: zipPath are not correct: \(zipPath)")
return
}
guard let unzipPath = folderDestination! else {
print("Error: unzipPath are not correct: \(unzipPath)")
return
}
let success = SSZipArchive.unzipFile(atPath: zipPath, toDestination: unzipPath)
if !success {
print("Error: unzipFile operation failed")
return
}

Simply you can't create folder name by appending the path, You need to create folder separately. Here is the code try this!
let filename = response.response?.suggestedFilename
var folderDestination=response.destinationURL?.path
folderDestination=folderDestination?.appending("/\(nameCategory)")
try! FileManager.default.createDirectory(at: folderDestination!, withIntermediateDirectories: false, attributes: nil)
archiveToUnzip=(folderDestination?.appending("/\(filename!)"))!
//unzip
let successUnZip=SSZipArchive.unzipFile(atPath: archiveToUnzip,toDestination:folderDestination!)

Related

PDF File reading Issue only on Offline mode swift

I have successfully Implemented downloading mechanism for pdf files. Files are downloading and save to document directory of simulator.
Issue is when I try to disconnect internet the files locally stored not accessible. In my application I have two modes Online and Offline. In Online mode I store data and its accessible at Offline mode. Only problem is getting with pdf files.
How to access pdf files in offline mode?
viewController Code:
let fileType = (URL != nil) ? ".pdf" : ".png"
DataProvider.main.downloadFile(url: String(describing: self.URL!), filetype: fileType, callback: { success, response in
if !success || response == nil {
return false
}
if((response as! String).hasSuffix("pdf")){
self.imageScrollView.isHidden = true
self.contentContainer.isHidden = false
self.document = ReaderDocument(filePath:(response as! String).removingPercentEncoding, password: nil)
self.loadDocument()
self._contentView.removeFromSuperview()
self._contentView.frame = self.contentContainer.bounds
self.contentContainer.addSubview(self._contentView)
}else if (FileManager.default.fileExists(atPath: (response as! String).removingPercentEncoding!) ){
self.imageScrollView.isHidden = false
self.contentContainer.isHidden = true
let image = UIImage(contentsOfFile:(response as! String).removingPercentEncoding!)!
self.imageScrollView.display(image)
}
return true
})
DataProvider Class with custom directory:
#objc public func downloadFile(url:String, filetype: String, callback:#escaping (_ success:Bool, _ result:Any?)->(Bool)) -> Void {
var destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
if filetype.elementsEqual(".pdf"){
destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent(String(abs(url.hashValue)) + ".pdf") //giving different name to file
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
}
Alamofire.download(
url,
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: [ "Accept":"*/*", "Authorization":"Bearer \(token ?? "")"],
to: destination).downloadProgress(closure: { (progress) in
//progress closure
print(progress)
}).response(completionHandler: { (DefaultDownloadResponse) in
//here you able to access the DefaultDownloadResponse
//result closure
callback(DefaultDownloadResponse.response?.statusCode == 200, DefaultDownloadResponse.destinationURL?.absoluteString.replacingOccurrences(of: "file://", with: ""))
print(DefaultDownloadResponse)
})
}
DefaultDownloadResponse.destinationURL when Online downloaded
▿ destinationURL : Optional<URL>
▿ some : file:///Users/macuser/Library/Developer/CoreSimulator/Devices/4B6C2727-46AB-4797-8CBB-D5A897867013/data/Containers/Data/Application/08441079-027E-4A5C-A504-4151A8405CF0/Documents/660444321507407187.pdf
- _url : file:///Users/macuser/Library/Developer/CoreSimulator/Devices/4B6C2727-46AB-4797-8CBB-D5A897867013/data/Containers/Data/Application/08441079-027E-4A5C-A504-4151A8405CF0/Documents/660444321507407187.pdf
when Offline its not downloaded or not picked from local
finished with error [-1009] Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x600002fd50e0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDownloadTask <98102CE1-D70F-4A0E-AB89-8CAAEFE29213>.<13>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDownloadTask <98102CE1-D70F-4A0E-AB89-8CAAEFE29213>.<13>"
), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=http://pdfv2.zta.com/Pdf/GetPageFromPdfUrl?url=http://d4static.zed.com/31/2019/8585/6999605/6999605.pdf&page=1,
Here I am checking if file exists or not first.
//Call it like this
checkIfFileExists(urlString: "your_url")
func checkIfFileExists(urlString: String) {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let fileName = urlString.fileName()
let fileManager = FileManager.default
let filePath = url.appendingPathComponent("\(fileName).pdf")?.path
print("filePath : \(String(describing: filePath))")
if fileManager.fileExists(atPath: filePath!) {
print("File exists")
} else {
print("File doesn't exists")
stackoverFlowTask()
}
}
func stackoverFlowTask() {
downloadFile(url: "your_url", filetype: ".pdf", callback: { success, response in
if !success || response == nil {
return false
}
return true
})
}
public func downloadFile(url:String, filetype: String, callback:#escaping (_ success:Bool, _ result:Any?)->(Bool)) -> Void {
var destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
if filetype.elementsEqual(".pdf"){
destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let downloadFileName = url.fileName()
let fileURL = documentsURL.appendingPathComponent("\(downloadFileName).pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
}
Alamofire.download(
url,
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: nil,
to: destination).downloadProgress(closure: { (progress) in
//progress closure
print(progress)
}).response(completionHandler: { (DefaultDownloadResponse) in
//here you able to access the DefaultDownloadResponse
//result closure
callback(DefaultDownloadResponse.response?.statusCode == 200, DefaultDownloadResponse.destinationURL?.absoluteString.replacingOccurrences(of: "file://", with: ""))
print(DefaultDownloadResponse)
})
}
Extension:
extension String {
func fileName() -> String {
return self.replacingOccurrences(of: "[/+.:.-=.%&]", with: "", options: .regularExpression, range: nil)
}
}

Not getting eject file after unzipping downloaded file URL

First of all, after successfully download from the URL using Alamofire, I am changing the file extension to.ZIP, then getting an error while unzipping.
Not getting expected file.
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
Alamofire.download(fileURL!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers, to: destination).downloadProgress(closure: { (progress) in
print(progress.completedUnitCount)
}).responseData { (responce) in
let destiUrl = responce.destinationURL
print(destiUrl!)
let newUrl = destiUrl?.deletingPathExtension().appendingPathExtension("zip")
do {
try FileManager.default.copyItem(at: destiUrl!, to: newUrl!)
let unzipDirectory = try Zip.quickUnzipFile(newUrl!)
print(unzipDirectory.absoluteString)
}
catch let error as NSError{
print(error)
}
}
File URL after successful download-->
file:///var/mobile/Containers/Data/Application/9D96958C-903E-4693-9965-6FB919BB24F1/Documents/'87dc4a8ddce24cf9ad35a251d6a98195.hub'
File URL after converting to .zip
file:///var/mobile/Containers/Data/Application/9D96958C-903E-4693-9965-6FB919BB24F1/Documents/'87dc4a8ddce24cf9ad35a251d6a98195.zip
Final url after unzipping
file:///var/mobile/Containers/Data/Application/9D96958C-903E-4693-9965-6FB919BB24F1/Documents/'87dc4a8ddce24cf9ad35a251d6a98195/
Actual result should be audio file.
Tried replacing the name of the file at the time of successfully download using below code. -->
func saveFileInDocDirectory(data: Data?, fileName: String?, successblock: #escaping (_ path: String?) -> Void) { // To add the image to cache for given identifier.
let paths = NSSearchPathForDirectoriesInDomains( .documentDirectory, .userDomainMask, true)[0] as String
let path = paths.appending("/\(fileName!)")
if (FileManager.default.fileExists(atPath: path)) {
try! FileManager.default.removeItem(atPath: path)
} else {
do {
try data?.write(to: URL(fileURLWithPath: path, isDirectory: false))
successblock(path)
} catch {
successblock(nil)
print("Error while caching the data in cache folder.")
}
}
}
And after that unzipped using SSZipArchive library in Alamofire download function -->
Alamofire.download(fileURL!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers, to: destination).downloadProgress(closure: { (progress) in
print(progress.completedUnitCount)
}).responseData { (responce) in
let destiUrl = responce.destinationURL
print(destiUrl!)
let name = destiUrl?.deletingPathExtension().lastPathComponent
self.saveFileInDocDirectory(data: responce.result.value, fileName: "\(name!).zip", successblock: { (path) in
print(path!)
var filepath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
let url = URL(fileURLWithPath: filepath)
do {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
let done = SSZipArchive.unzipFile(atPath: path!, toDestination: url.path)
if done{
let items = try FileManager.default.contentsOfDirectory(atPath: url.path)
print(items)
let destinationUrl = url.appendingPathComponent(items[0])
print(destinationUrl)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
player = AVQueuePlayer(url: destinationUrl)
player.play()
}
} catch let error as NSError{
print(error)
}
})
}

Swift How to Unzip file from URL with Alamofire and SSZipArchive

func downLoad(fileName:String) {
let urlString : String = "\(myurl)\(fileName)"
var localPath: NSURL?
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
Alamofire.download(urlString, method: .get, encoding: JSONEncoding.default, to: destination)
.downloadProgress(queue: DispatchQueue.global(qos: .utility)) { progress in
print("Progress: \(progress.fractionCompleted)")
}
.validate { request, response, temporaryURL, destinationURL in
// Custom evaluation closure now includes file URLs (allows you to parse out error messages if necessary)
return .success
}
.responseJSON { response in
debugPrint(response)
print(response.destinationURL?.path)
print(response.destinationURL?.absoluteString)
let unzipDirectory = self.unzipPath(fileURL:fileName)
let success = SSZipArchive.unzipFile(atPath: (response.destinationURL?.path)!, toDestination: unzipDirectory!)
print(success)
if !success {
return
}
}
}
func unzipPath(fileName:String) -> String? {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let pathComponent = url.appendingPathComponent("test\(fileName)")
do {
try FileManager.default.createDirectory(at: pathComponent!, withIntermediateDirectories: true, attributes: nil)
} catch {
return nil
}
return pathComponent?.absoluteString
}
i get right path in response.destinationURL
but success is false
i tried atPath : to response.destinationURL?.path and
response.destinationURL?.absoluteString
but failed too
What i am doing wrong
destinationURL?.path return this
Optional("/Users/MyUser/Library/Developer/CoreSimulator/Devices/3FBAD207-E5AB-4FC1-8199-2269A1249D97/data/Containers/Data/Application/CB1C2EF5-3100-430B-B869-774C09B8EA7F/Documents/testFile.zip")
response.destinationURL?.absoluteString
return this
Optional("file:///Users/MyUser/Library/Developer/CoreSimulator/Devices/3FBAD207-E5AB-4FC1-8199-2269A1249D97/data/Containers/Data/Application/CB1C2EF5-3100-430B-B869-774C09B8EA7F/Documents/testFile.zip")
i think this is correct URL
why failed unzip?
By looking at your code, the path you are using for the source is perfect.
For the destination path, try updating it to
func unzipPath(fileName:String) -> String? {
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
let pathWithComponent = path.appendingPathComponent("test\(fileName)")
do {
try FileManager.default.createDirectory(atPath: pathWithComponent, withIntermediateDirectories: true, attributes: nil)
} catch {
return nil
}
return pathWithComponent
}
Try and share the results.

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?
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:
Swift 4.0
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
documentsURL.appendPathComponent("file.csv")
return (documentsURL, [.removePreviousFile])
}
Alamofire.download(url, to: destination).responseData { response in
if let destinationUrl = response.destinationURL {
print("destinationUrl \(destinationUrl.absoluteURL)")
}
}
There are several enhancements in Alamofire 4. The first of which is the optionality of the destination closure. Now, by default, the destination closure is nil which means the file is not moved anywhere on the file system and the temporary URL is returned.
This is the default execution:-
Alamofire.download(urlString).responseData { response in
print("Temporary URL: \(response.temporaryURL)")
}
This is my code to download file with Alamofire 4.0 which return destination Url of file:-
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
documentsURL.appendPathComponent("duck.png")
return (documentsURL, [.removePreviousFile])
}
Alamofire.download(url, to: destination).responseData { response in
if let destinationUrl = response.destinationURL ? {
completionHandler(destinationUrl)
}
}
Downloading mp3 file with Alamofire 4.0 Swift 4.x
Since almost all samples seems to be about downloading an image or a JSON file, it took me hours to find the right solution.
I will share it here hoping it would help others to save some time.
func startDownload(audioUrl:String) -> Void {
let fileUrl = self.getSaveFileUrl(fileName: audioUrl)
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
return (fileUrl, [.removePreviousFile, .createIntermediateDirectories])
}
Alamofire.download(audioUrl, to:destination)
.downloadProgress { (progress) in
self.progressLabel.text = (String)(progress.fractionCompleted)
}
.responseData { (data) in
self.progressLabel.text = "Completed!"
}
}
func getSaveFileUrl(fileName: String) -> URL {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let nameUrl = URL(string: fileName)
let fileURL = documentsURL.appendingPathComponent((nameUrl?.lastPathComponent)!)
NSLog(fileURL.absoluteString)
return fileURL;
}
For the latest versions this is how it should look like:
let destination: DownloadRequest.Destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .picturesDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("image.png")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
AF.download("https://httpbin.org/image/png", to: destination).response { response in
debugPrint(response)
if response.error == nil, let imagePath = response.fileURL?.path {
let image = UIImage(contentsOfFile: imagePath)
}
}
Swift 3 Alamofire (4.4.0):
.plist add key "App Transport Security Settings->Allow Arbitrary Loads->Yes" if you copy and paste code below:
import Alamofire
let destination = DownloadRequest.suggestedDownloadDestination()
Alamofire.download("http://zmp3-mp3-lossless-te-zmp3-bdhcm-1.zadn.vn/151e407bb43f5d61042e/1223048424027738068?key=f-zMo3GZKlhVibnvGMsMuQ&expires=1495726053&filename=See%20You%20Again%20-%20Wiz%20Khalifa%20Charlie%20Puth%20(NhacPro.net).flac", to: destination).downloadProgress(queue: DispatchQueue.global(qos: .utility)) { (progress) in
print("Progress: \(progress.fractionCompleted)")
} .validate().responseData { ( response ) in
print(response.destinationURL!.lastPathComponent)
}
Use this code for download file
let fileManager = FileManager.default
let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
Alamofire.request(\(downloadUrl)).downloadProgress(closure : { (progress) in
print(progress.fractionCompleted)
}).responseData{ (response) in
print(response)
print(response.result.value!)
print(response.result.description)
let randomString = NSUUID().uuidString
if let data = response.result.value {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let videoURL = documentsURL.appendingPathComponent("\(randomString)")
do {
try data.write(to: videoURL)
} catch {
print("Something went wrong!")
}
}
}

Check if a file already exists before downloading it with Alamofire + suggestedDownloadDestination

How can I check if a given file has been already downloaded before re-downloading it by using Alamofire? I'm using suggestedDownloadDestination so Alamofire will automatically choose the name of the file and save it in the choosen directory, for example the .CachesDirectory. The problem is that the value given by suggestedDownloadDestination is of type DownloadFileDestination which will return a NSURL only by calling him with the request's response, but in this way I could not ever know the file path without performing a request before.
This is the code I currently use to download a file with Alamofire:
Alamofire.download(.GET, downloadLink, destination: destination).progress {
bytesRead, totalBytesRead, totalBytesExpectedToRead in
}.response {
request, response, data, error in
guard error == nil else {
return
}
// This will give me the file path but we're already in a Request!
print("\(destination(NSURL(string: "")!, response!))")
}
What am I missing?
Not sure if you figured this out yet, but you can create an extension over Alamofire.DownloadRequest like:
extension Alamofire.DownloadRequest {
open class func suggestedDownloadDestination(
for directory: FileManager.SearchPathDirectory = .documentDirectory,
in domain: FileManager.SearchPathDomainMask = .userDomainMask,
with options: DownloadOptions)
-> DownloadFileDestination
{
return { temporaryURL, response in
let destination = DownloadRequest.suggestedDownloadDestination(for: directory, in: domain)(temporaryURL, response)
return (destination.destinationURL, options)
}
}
}
Now you can specify in the options parameter if you want the file to be overwritten:
let destination = DownloadRequest.suggestedDownloadDestination(for: .cachesDirectory,
in: .userDomainMask,
with: [DownloadRequest.DownloadOptions.removePreviousFile])
Here is the solution which I used
Download a file
check if it exist if so just return the path otherwise
func downloadDocumentFile(filePath: String,onDownloadProgress: #escaping(_ progress: Double) -> Void,onError: #escaping(_ errorMessage: String) -> Void,onSuccess: #escaping(_ destinationUrl: URL) -> Void){
guard let url = URL(string: filePath) else {
onError("Couldn't create url from passed file path")
assertionFailure()
return
}
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory, in: .userDomainMask)
Alamofire.download(url, to: destination)
.downloadProgress { (progress) in
onDownloadProgress(progress.fractionCompleted)
}
.responseData(queue: .main) { (response) in
switch response.result {
case .success:
if let destinationUrl = response.destinationURL {
onSuccess(destinationUrl)
}else {
onError("Couldn't get destination url")
assertionFailure()
}
case .failure(let error):
// check if file exists before
if let destinationURL = response.destinationURL {
if FileManager.default.fileExists(atPath: destinationURL.path){
// File exists, so no need to override it. simply return the path.
onSuccess(destinationURL)
print()
}else {
onError(error.localizedDescription)
assertionFailure()
}
}else {
onError(error.localizedDescription)
assertionFailure()
}
}
}
}

Resources