I am trying to download file content from server, creating one file on client end and tranfering all content to that. I am saving the file to local storage,but it is unable to save throws exception that you don't have permission to save in folder. Tried so many things but unable to do so. I am very new to this ios programming. Please help me out, thanks in advance.
Below is my code:
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
var fileURL : URL = URL(fileURLWithPath: documentsUrl!.path+filename)
fileURL = URL(fileURLWithPath: documentsUrl!.path+filename+".doc")
let data = NSData(contentsOf: tempLocalUrl)
do {
try data?.write(to: fileURL, options: .atomic)
} catch {
print(error,"helooooooooooooooooo")
}
I have used relativepath, absolutestring but still no luck .
This is the error :
Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “DocumentsImport From IE.doc” in the folder “0103E60B-478F-475B-A8F2-788534F5D206”." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/0103E60B-478F-475B-A8F2-788534F5D206/DocumentsImport From IE.doc, NSUnderlyingError=0x1076efe90 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
I Save my image like that
let fileManager = FileManager.default
do {
let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
let fileURL = documentDirectory.appendingPathComponent(name)
let image = #imageLiteral(resourceName: "Notifications")if let imageData = UIImageJPEGRepresentation(image, 0.5) {
try imageData.write(to: fileURL)
return true
}
} catch {
print(error)
}