From 407b801243c6b101b0282dceea5d35a24381f325 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Mon, 18 Dec 2017 23:14:27 -0600 Subject: [PATCH] Fixes issue where importing via UIDocumentBrowserViewController would fail due to invalid permissions --- Delta/Importing/ImportController.swift | 31 +++++++++++++++----------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/Delta/Importing/ImportController.swift b/Delta/Importing/ImportController.swift index a394054..ce04a87 100644 --- a/Delta/Importing/ImportController.swift +++ b/Delta/Importing/ImportController.swift @@ -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()