Fix the permission denied issue

This commit is contained in:
Yishi Lin 2020-02-27 01:17:39 +08:00
parent 6b3ce819db
commit 7d71b2f221
2 changed files with 23 additions and 3 deletions

View file

@ -50,6 +50,15 @@ extension PGPKeyFileImportTableViewController: UIDocumentPickerDelegate {
} }
let fileName = url.lastPathComponent let fileName = url.lastPathComponent
do { 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) let fileContent = try String(contentsOf: url, encoding: .ascii)
switch currentlyPicking { switch currentlyPicking {
case .none: case .none:
@ -62,7 +71,8 @@ extension PGPKeyFileImportTableViewController: UIDocumentPickerDelegate {
pgpPrivateKeyFile.textLabel?.text = fileName pgpPrivateKeyFile.textLabel?.text = fileName
} }
} catch { } 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)
} }
} }
} }

View file

@ -21,7 +21,7 @@ class SSHKeyFileImportTableViewController: AutoCellHeightUITableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: 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 cell?.isSelected = false
guard cell == sshPrivateKeyFile else { guard cell == sshPrivateKeyFile else {
return return
@ -42,10 +42,20 @@ extension SSHKeyFileImportTableViewController: UIDocumentPickerDelegate {
} }
let fileName = url.lastPathComponent let fileName = url.lastPathComponent
do { 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) privateKey = try String(contentsOf: url, encoding: .ascii)
sshPrivateKeyFile.textLabel?.text = fileName sshPrivateKeyFile.textLabel?.text = fileName
} catch { } 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)
} }
} }
} }