From 3cc2182bc0045ae9cc8541a11a87b3f45651476e Mon Sep 17 00:00:00 2001 From: Yishi Lin Date: Tue, 25 Sep 2018 00:51:18 +0800 Subject: [PATCH] Fix #213: app now clears clipboard in 45s --- .../Controllers/PasswordsViewController.swift | 2 +- pass/Helpers/SecurePasteboard.swift | 31 ++++++------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/pass/Controllers/PasswordsViewController.swift b/pass/Controllers/PasswordsViewController.swift index f2c6fc7..cc901db 100644 --- a/pass/Controllers/PasswordsViewController.swift +++ b/pass/Controllers/PasswordsViewController.swift @@ -406,7 +406,7 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV decryptedPassword = try self.passwordStore.decrypt(passwordEntity: passwordEntity, requestPGPKeyPassphrase: self.requestPGPKeyPassphrase) DispatchQueue.main.async { SecurePasteboard.shared.copy(textToCopy: decryptedPassword?.password) - SVProgressHUD.showSuccess(withStatus: "Password copied, and will be cleared in 45 seconds.") + SVProgressHUD.showSuccess(withStatus: "Password copied. We will clear the pasteboard in 45 seconds.") SVProgressHUD.dismiss(withDelay: 0.6) } } catch { diff --git a/pass/Helpers/SecurePasteboard.swift b/pass/Helpers/SecurePasteboard.swift index 04541b6..df4f1f8 100644 --- a/pass/Helpers/SecurePasteboard.swift +++ b/pass/Helpers/SecurePasteboard.swift @@ -11,7 +11,7 @@ import UIKit class SecurePasteboard { public static let shared = SecurePasteboard() - private var backgroundTaskID: UIBackgroundTaskIdentifier? = nil + private var backgroundTaskID = UIBackgroundTaskInvalid func copy(textToCopy: String?, expirationTime: Double = 45) { // copy to the pasteboard @@ -23,32 +23,21 @@ class SecurePasteboard { } // exit the existing background task, if any - if let backgroundTaskID = backgroundTaskID { - UIApplication.shared.endBackgroundTask(backgroundTaskID) - self.backgroundTaskID = nil + if backgroundTaskID != UIBackgroundTaskInvalid { + UIApplication.shared.endBackgroundTask(UIBackgroundTaskInvalid) + self.backgroundTaskID = UIBackgroundTaskInvalid } backgroundTaskID = UIApplication.shared.beginBackgroundTask(expirationHandler: { [weak self] in - guard let taskID = self?.backgroundTaskID else { - return - } - if textToCopy == UIPasteboard.general.string { - UIPasteboard.general.string = "" - } - UIApplication.shared.endBackgroundTask(taskID) + UIPasteboard.general.string = "" + UIApplication.shared.endBackgroundTask(UIBackgroundTaskInvalid) + self?.backgroundTaskID = UIBackgroundTaskInvalid }) DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + expirationTime) { [weak self] in - guard let strongSelf = self else { - return - } - if textToCopy == UIPasteboard.general.string { - UIPasteboard.general.string = "" - } - if let backgroundTaskID = strongSelf.backgroundTaskID { - UIApplication.shared.endBackgroundTask(backgroundTaskID) - strongSelf.backgroundTaskID = nil - } + UIPasteboard.general.string = "" + UIApplication.shared.endBackgroundTask(UIBackgroundTaskInvalid) + self?.backgroundTaskID = UIBackgroundTaskInvalid } }