I'm trying to import a custom file from one of my apps to another one (let's call them App_A and App_B). If I export the file directly from App_A and in the share sheet I select App_B everything works fine.
My problem is that the file is saved in iCloud Drive so the user can access it through the system's File app. When I try to select the same file from Files app or in App_B through UIDocumentPickerViewController
.
In both cases I run the following code (inside UIDocumentPickerDelegate
or AppDelegate
):
let access = url.startAccessingSecurityScopedResource()
var error: NSError? = nil
NSFileCoordinator().coordinate(readingItemAt: url, error: &error) { (coorURL) in
do{
let data = try Data(contentsOf: coorURL)
}catch{
print("manage error: \(error.localizedDescription)")
}
if access{
url.stopAccessingSecurityScopedResource()
}
}
The error I get is always the same:
Error Domain=NSCocoaErrorDomain Code=257 ... NSUnderlyingError=0x2803e07b0 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}
and url.startAccessingSecurityScopedResource()
returns always false
.
I'm doing this on Xcode 11.4.1, Swift 5, iOS 13.3.1 on device and 13.4.1 on simulator, but the result doesn't change.
Do you have an idea about this weird error? I suppose that if the user select a file and an app to open it the permission would be granted, but probably I'm missing something. Could you help?