diff --git a/deltachat-ios/Controller/Settings/ChatsAndMediaViewController.swift b/deltachat-ios/Controller/Settings/ChatsAndMediaViewController.swift index 818733b9d..2d30b4d20 100644 --- a/deltachat-ios/Controller/Settings/ChatsAndMediaViewController.swift +++ b/deltachat-ios/Controller/Settings/ChatsAndMediaViewController.swift @@ -1,7 +1,6 @@ import UIKit import DcCore import Intents -import LocalAuthentication internal final class ChatsAndMediaViewController: UITableViewController { @@ -158,7 +157,10 @@ internal final class ChatsAndMediaViewController: UITableViewController { case .mediaQuality: showMediaQuality() case .downloadOnDemand: showDownloadOnDemand() case .receiptConfirmation: break - case .exportBackup: authenticateAndCreateBackup() + case .exportBackup: + Utils.authenticateDeviceOwner(reason: String.localized("pref_backup_explain")) { [weak self] in + self?.createBackup() + } } } @@ -173,24 +175,7 @@ internal final class ChatsAndMediaViewController: UITableViewController { // MARK: - actions private func authenticateAndCreateBackup() { - let localAuthenticationContext = LAContext() - var error: NSError? - if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) { - let reason = String.localized("pref_backup_explain") - localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { [weak self] success, error in - DispatchQueue.main.async { - guard let self = self else { return } - if success { - self.createBackup() - } else { - logger.info("local authentication aborted: \(String(describing: error))") - } - } - } - } else { - logger.info("local authentication unavailable: \(String(describing: error))") - createBackup() - } + } private func createBackup() { diff --git a/deltachat-ios/Controller/Settings/SettingsViewController.swift b/deltachat-ios/Controller/Settings/SettingsViewController.swift index a824327ca..490468781 100644 --- a/deltachat-ios/Controller/Settings/SettingsViewController.swift +++ b/deltachat-ios/Controller/Settings/SettingsViewController.swift @@ -1,7 +1,6 @@ import UIKit import DcCore import Intents -import LocalAuthentication internal final class SettingsViewController: UITableViewController { @@ -288,23 +287,8 @@ internal final class SettingsViewController: UITableViewController { title: String.localized("perm_continue"), style: .default, handler: { [weak self] _ in - guard let self else { return } - let localAuthenticationContext = LAContext() - var error: NSError? - if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) { - let reason = String.localized("multidevice_this_creates_a_qr_code") - localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { [weak self] success, error in - DispatchQueue.main.async { - guard let self = self else { return } - if success { - self.navigationController?.pushViewController(BackupTransferViewController(dcAccounts: self.dcAccounts), animated: true) - } else { - logger.info("local authentication aborted: \(String(describing: error))") - } - } - } - } else { - logger.info("local authentication unavailable: \(String(describing: error))") + Utils.authenticateDeviceOwner(reason: String.localized("multidevice_this_creates_a_qr_code")) { [weak self] in + guard let self else { return } self.navigationController?.pushViewController(BackupTransferViewController(dcAccounts: self.dcAccounts), animated: true) } } diff --git a/deltachat-ios/Helper/Utils.swift b/deltachat-ios/Helper/Utils.swift index 97e3765b1..2e2394f81 100644 --- a/deltachat-ios/Helper/Utils.swift +++ b/deltachat-ios/Helper/Utils.swift @@ -1,7 +1,7 @@ import Foundation import UIKit import DcCore - +import LocalAuthentication extension URL { var isDeltaChatInvitation: Bool { @@ -129,4 +129,23 @@ struct Utils { activityVC.popoverPresentationController?.sourceView = sourceView // iPad crashes without a source parentViewController.present(activityVC, animated: true, completion: nil) } + + public static func authenticateDeviceOwner(reason: String, callback: @escaping () -> Void) { + let localAuthenticationContext = LAContext() + var error: NSError? + if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &error) { + localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { success, error in + DispatchQueue.main.async { + if success { + callback() + } else { + logger.info("local authentication aborted: \(String(describing: error))") + } + } + } + } else { + logger.info("local authentication unavailable: \(String(describing: error))") + callback() + } + } }