Last active
January 16, 2018 17:50
-
-
Save dreymonde/6ea87b9b4a5cfbbfcaf4d197142d7369 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct UserConfirmationRequired { | |
private let performDestructiveAction: () -> () | |
init(destructiveAction: @escaping () -> ()) { | |
self.performDestructiveAction = destructiveAction | |
} | |
func performWithUserConfirmation(alertTitle: String, | |
alertMessage: String, | |
alertDestructiveActionTitle: String, | |
completion: @escaping (Bool) -> ()) { | |
// retrieving view controller to show alert from | |
guard let window = UIApplication.shared.delegate?.window else { | |
print("No window") | |
completion(false) | |
return | |
} | |
guard let viewController = window?.rootViewController else { | |
print("No view controller") | |
completion(false) | |
return | |
} | |
// creating and showing an alert | |
let alert = UIAlertController(title: alertTitle, | |
message: alertMessage, | |
preferredStyle: .actionSheet) | |
let cancel = UIAlertAction(title: "Cancel", | |
style: .cancel, | |
handler: { _ in completion(false) }) | |
let destructive = UIAlertAction(title: alertDestructiveActionTitle, | |
style: .destructive, | |
handler: { _ in | |
self.performDestructiveAction() | |
completion(true) | |
}) | |
alert.addAction(cancel) | |
alert.addAction(destructive) | |
viewController.present(alert, animated: true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment