From 7d71b2f221678da12061c0b79894fa3f56d3a88a Mon Sep 17 00:00:00 2001 From: Yishi Lin Date: Thu, 27 Feb 2020 01:17:39 +0800 Subject: [PATCH] Fix the permission denied issue --- .../PGPKeyFIleImportTableViewController.swift | 12 +++++++++++- .../SSHKeyFileImportTableViewController.swift | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pass/Controllers/PGPKeyFIleImportTableViewController.swift b/pass/Controllers/PGPKeyFIleImportTableViewController.swift index d182f5f..a6c938f 100644 --- a/pass/Controllers/PGPKeyFIleImportTableViewController.swift +++ b/pass/Controllers/PGPKeyFIleImportTableViewController.swift @@ -50,6 +50,15 @@ extension PGPKeyFileImportTableViewController: UIDocumentPickerDelegate { } let fileName = url.lastPathComponent do { + // Start accessing a security-scoped resource. + guard url.startAccessingSecurityScopedResource() else { + // Handle the failure here. + throw AppError.ReadingFile(fileName) + } + + // Make sure you release the security-scoped resource when you are done. + defer { url.stopAccessingSecurityScopedResource() } + let fileContent = try String(contentsOf: url, encoding: .ascii) switch currentlyPicking { case .none: @@ -62,7 +71,8 @@ extension PGPKeyFileImportTableViewController: UIDocumentPickerDelegate { pgpPrivateKeyFile.textLabel?.text = fileName } } catch { - Utils.alert(title: "CannotImportFile".localize(), message: "FileCannotBeImported.".localize(fileName), controller: self) + let message = "FileCannotBeImported.".localize(fileName) | "UnderlyingError".localize(error.localizedDescription) + Utils.alert(title: "CannotImportFile".localize(), message: message, controller: self) } } } diff --git a/pass/Controllers/SSHKeyFileImportTableViewController.swift b/pass/Controllers/SSHKeyFileImportTableViewController.swift index 944b31c..3c6f4dc 100644 --- a/pass/Controllers/SSHKeyFileImportTableViewController.swift +++ b/pass/Controllers/SSHKeyFileImportTableViewController.swift @@ -21,7 +21,7 @@ class SSHKeyFileImportTableViewController: AutoCellHeightUITableViewController { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) - let picker = UIDocumentPickerViewController(documentTypes: ["public.data"], in: .open) + let picker = UIDocumentPickerViewController(documentTypes: ["public.data"], in: .import) cell?.isSelected = false guard cell == sshPrivateKeyFile else { return @@ -42,10 +42,20 @@ extension SSHKeyFileImportTableViewController: UIDocumentPickerDelegate { } let fileName = url.lastPathComponent do { + // Start accessing a security-scoped resource. + guard url.startAccessingSecurityScopedResource() else { + // Handle the failure here. + throw AppError.ReadingFile(fileName) + } + + // Make sure you release the security-scoped resource when you are done. + defer { url.stopAccessingSecurityScopedResource() } + privateKey = try String(contentsOf: url, encoding: .ascii) sshPrivateKeyFile.textLabel?.text = fileName } catch { - Utils.alert(title: "CannotImportFile".localize(), message: "FileCannotBeImported.".localize(fileName), controller: self) + let message = "FileCannotBeImported.".localize(fileName) | "UnderlyingError".localize(error.localizedDescription) + Utils.alert(title: "CannotImportFile".localize(), message: message, controller: self) } } }