-
Notifications
You must be signed in to change notification settings - Fork 6
/
LeakInspectorAlertProvider.swift
56 lines (45 loc) · 2.1 KB
/
LeakInspectorAlertProvider.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import UIKit
@objc class LeakInspectorAlertProvider: NSObject, LeakInspectorDelegate {
private weak var alertController: UIAlertController?
func didLeakReference(_ ref: AnyObject, name: String) {
// dismiss any already visible alert
if let alertController = self.alertController {
alertController.dismiss(animated: false, completion: nil)
}
let title = "Leak Inspector"
let message = "Detected possible leak of \(name)"
let ok = "OK"
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: ok, style: .default, handler: nil))
alertController.show()
self.alertController = alertController
}
}
extension UIAlertController {
func show() {
present(animated: true, completion: nil)
}
func present(animated: Bool, completion: (() -> Void)?) {
let keyWindow: UIWindow? = {
if #available(iOS 13, *) {
return UIApplication.shared.windows.first { $0.isKeyWindow }
} else {
return UIApplication.shared.keyWindow
}
}()
if let rootVC = keyWindow?.rootViewController {
present(from: rootVC, animated: animated, completion: completion)
}
}
private func present(from controller: UIViewController, animated: Bool, completion: (() -> Void)?) {
if let presentedViewController = controller.presentedViewController {
present(from: presentedViewController, animated: animated, completion: completion)
} else if let navVC = controller as? UINavigationController, let visibleVC = navVC.visibleViewController {
present(from: visibleVC, animated: animated, completion: completion)
} else if let tabVC = controller as? UITabBarController, let selectedVC = tabVC.selectedViewController {
present(from: selectedVC, animated: animated, completion: completion)
} else {
controller.present(self, animated: animated, completion: completion)
}
}
}