75 lines
3.1 KiB
Swift
75 lines
3.1 KiB
Swift
//
|
|
// Alert++.swift
|
|
// Cowabunga
|
|
//
|
|
// Created by sourcelocation on 30/01/2023.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
// credit: sourcelocation & TrollTools
|
|
var currentUIAlertController: UIAlertController?
|
|
var lastDismissTitle: String?
|
|
|
|
|
|
fileprivate let errorString = NSLocalizedString("Error", comment: "")
|
|
fileprivate let okString = NSLocalizedString("OK", comment: "")
|
|
fileprivate let cancelString = NSLocalizedString("Cancel", comment: "")
|
|
|
|
extension UIApplication {
|
|
|
|
func dismissAlert(animated: Bool) {
|
|
DispatchQueue.main.async {
|
|
lastDismissTitle = currentUIAlertController?.title
|
|
currentUIAlertController?.dismiss(animated: animated)
|
|
}
|
|
}
|
|
func alert(title: String = errorString, body: String, animated: Bool = true, withButton: Bool = true) {
|
|
DispatchQueue.main.async {
|
|
var body = body
|
|
|
|
if title == errorString {
|
|
// append debug info
|
|
let device = UIDevice.current
|
|
let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
|
|
let appBuild = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? ""
|
|
let systemVersion = device.systemVersion
|
|
body += "\n\(device.systemName) \(systemVersion), v\(appVersion) build \(appBuild)"
|
|
}
|
|
|
|
currentUIAlertController = UIAlertController(title: title, message: body, preferredStyle: .alert)
|
|
if withButton { currentUIAlertController?.addAction(.init(title: okString, style: .cancel)) }
|
|
self.present(alert: currentUIAlertController!)
|
|
}
|
|
}
|
|
func confirmAlert(title: String = errorString, body: String, confirmTitle: String = okString, onOK: @escaping () -> (), noCancel: Bool) {
|
|
DispatchQueue.main.async {
|
|
currentUIAlertController = UIAlertController(title: title, message: body, preferredStyle: .alert)
|
|
if !noCancel {
|
|
currentUIAlertController?.addAction(.init(title: cancelString, style: .cancel))
|
|
}
|
|
currentUIAlertController?.addAction(.init(title: confirmTitle, style: noCancel ? .cancel : .default, handler: { _ in
|
|
onOK()
|
|
}))
|
|
self.present(alert: currentUIAlertController!)
|
|
}
|
|
}
|
|
func change(title: String = errorString, body: String) {
|
|
DispatchQueue.main.async {
|
|
currentUIAlertController?.title = title
|
|
currentUIAlertController?.message = body
|
|
}
|
|
}
|
|
|
|
func present(alert: UIAlertController) {
|
|
if let window = UIApplication.shared.connectedScenes.map({ $0 as? UIWindowScene }).compactMap({ $0 }).first?.windows.first, var topController = window.rootViewController {
|
|
while let presentedViewController = topController.presentedViewController, presentedViewController.title != lastDismissTitle {
|
|
topController = presentedViewController
|
|
}
|
|
|
|
topController.present(alert, animated: true)
|
|
// topController should now be your topmost view controller
|
|
}
|
|
}
|
|
}
|