Hi there. I am developing a mac os application where i have to convert the data from server API to a zip file. The API is returning a zip file itself in encoded format of type Data, but i want to convert that data to a zip file and want to store in the disk.
CODE:
______
func DownloadExamZip(){
let request = NSMutableURLRequest(url: NSURL(string: "http://localhost:5000/api/DownloadExamZip/EX0000018/ST000000195/874059")! as URL)
request.httpMethod = "GET"
let AuthorizationToken = "Hidden"
request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.setValue(AuthorizationToken, forHTTPHeaderField: "Authorization")
let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
do {
guard data != nil else {
print("data is nil")
return
}
//Here i want to convert the type data to a zip file
}
catch {
print("Error -> \(error)")
}
}
task.resume()
}
Can anyone help me to convert that data into a zip file please. I also have to store that file in the disk.
You can get a URL for the Documents directory using
FileManager
.
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
Be aware that this only works the way you expect it to work if your app is not sandboxed. If your app is sandboxed — and all Mac App Store apps must be sandboxed — things get more complex. Let me know if that’s the case and I can go into the details.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"