Skip to content

Instantly share code, notes, and snippets.

@dreymonde
Last active January 16, 2018 17:50
Show Gist options
  • Save dreymonde/6ea87b9b4a5cfbbfcaf4d197142d7369 to your computer and use it in GitHub Desktop.
Save dreymonde/6ea87b9b4a5cfbbfcaf4d197142d7369 to your computer and use it in GitHub Desktop.
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