Reputation: 990
I need to delete a file from my app and i used below code got the error **
couldn’t be removed because you don’t have permission to access it.
**
let fileManager = NSFileManager()
do {
let attr : NSDictionary? = try NSFileManager.defaultManager().attributesOfItemAtPath(NSURL(string: fileModule.filepath)!.path!)
if let _attr = attr {
let newsize : UInt64 = fileSize
fileSize = _attr.fileSize() + newsize;
}
try fileManager.removeItemAtURL(NSURL(string: fileModule.filepath)!)
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
How remove a file in sandboxing enable state with permission?
Upvotes: 0
Views: 575
Reputation: 53000
In general for a sandboxed app to access a file for reading, writing or deletion it must be given permission to do so by the user. In your case you can use a standard file open dialog, which you can customise if you wish, as an "ask for permission to access" dialog. If your user selects the file, and customisation of the dialog can be used to assist them in doing so, you can use the returned NSURL to delete the file. To use the file open dialog you must give your app the appropriate entitlement.
An SO or wider search should turn up sample code if you need it.
HTH
Upvotes: 1