Fixes issue where importing via UIDocumentBrowserViewController would fail due to invalid permissions

This commit is contained in:
Riley Testut 2017-12-18 23:14:27 -06:00
parent a5acf30600
commit 407b801243

View File

@ -194,25 +194,30 @@ extension ImportController: UIDocumentBrowserViewControllerDelegate
let intent = NSFileAccessIntent.readingIntent(with: url)
self.fileCoordinator.coordinate(with: [intent], queue: self.importQueue) { (error) in
if let error = error
do
{
errors.append(error)
}
else
{
let temporaryURL = FileManager.default.temporaryDirectory.appendingPathComponent(url.lastPathComponent)
do
if let error = error
{
// Always access intent.url, as the system may have updated it when requesting access.
throw error
}
else
{
// User intent.url, not url, as the system may have updated it when requesting access.
guard intent.url.startAccessingSecurityScopedResource() else { throw CocoaError.error(.fileReadNoPermission) }
defer { intent.url.stopAccessingSecurityScopedResource() }
// Use url, not intent.url, to ensure the file name matches what was in the document browser.
let temporaryURL = FileManager.default.temporaryDirectory.appendingPathComponent(url.lastPathComponent)
try FileManager.default.copyItem(at: intent.url, to: temporaryURL)
coordinatedURLs.insert(temporaryURL)
}
catch
{
errors.append(error)
}
}
catch
{
errors.append(error)
}
dispatchGroup.leave()