forked from Zero-zhou/iOS-Extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlertController.swift
202 lines (135 loc) · 7.68 KB
/
AlertController.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// AlertController.swift
//
// Created by Augus on 2/3/16.
// Copyright © 2016 iAugus. All rights reserved.
//
import UIKit
// MARK: -
private var xoAssociationKey: UInt8 = 0
extension UIAlertController {
func addActions(_ actions: [UIAlertAction]) {
actions.forEach({
self.addAction($0)
})
}
func addActions(_ actions: UIAlertAction...) {
addActions(actions)
}
fileprivate var alertWindow: UIWindow? {
get {
return objc_getAssociatedObject(self, &xoAssociationKey) as? UIWindow
}
set {
objc_setAssociatedObject(self, &xoAssociationKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
func show(animated a: Bool = true, hideStatusBar: Bool = false, statusBarStyle: UIStatusBarStyle? = nil, completion: Closure? = nil) {
alertWindow = UIWindow(frame: UIScreen.main.bounds)
alertWindow?.backgroundColor = .clear
alertWindow?.windowLevel = UIWindowLevelAlert
let vc = BlankViewController()
if let _w = UIApplication.shared.delegate?.window, let w = _w {
alertWindow?.tintColor = w.tintColor
vc.isStatusBarHidden = hideStatusBar
if let style = w.rootViewController?.preferredStatusBarStyle {
vc.statusBarStyle = style
}
}
statusBarStyle != nil ? vc.statusBarStyle = statusBarStyle : ()
alertWindow?.rootViewController = vc
alertWindow?.makeKeyAndVisible()
alertWindow?.rootViewController?.present(self, animated: a, completion: completion)
}
open override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
alertWindow?.removeFromSuperview()
alertWindow = nil
}
}
private class BlankViewController: UIViewController {
var statusBarStyle: UIStatusBarStyle! = .default {
didSet {
setNeedsStatusBarAppearanceUpdate()
}
}
var isStatusBarHidden: Bool = false
fileprivate var tempIsStatusBarHidden: Bool?
fileprivate override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
// hide status bar in landscape on iPhone if needed
tempIsStatusBarHidden == nil ? tempIsStatusBarHidden = isStatusBarHidden : ()
guard !UIDevice.current.isPad && !tempIsStatusBarHidden! else { return }
isStatusBarHidden = toInterfaceOrientation.isLandscape
}
fileprivate override var prefersStatusBarHidden: Bool {
return isStatusBarHidden
}
fileprivate override var preferredStatusBarStyle: UIStatusBarStyle {
return statusBarStyle
}
}
// MARK: -
private let ok = NSLocalizedString("OK", comment: "")
private let cancel = NSLocalizedString("Cancel", comment: "")
class AlertController {
typealias CompletionHandler = (Bool) -> ()
class func alert(_ title: String = "", message: String = "", actionTitle: String = ok, baseViewController: UIViewController? = nil, tintColor: UIColor? = nil, closure: CompletionHandler?) {
alert(title, message: message, actionTitle: actionTitle, addCancelAction: false, baseViewController: baseViewController, tintColor: tintColor, closure: closure)
}
class func configured(_ title: String = "", message: String = "", actionTitle: String = ok, tintColor: UIColor? = nil, closure: CompletionHandler?) -> UIAlertController {
return configured(title, message: message, actionTitle: actionTitle, addCancelAction: false, tintColor: tintColor, closure: closure)
}
class func alertWithCancelAction(_ title: String = "", message: String = "", actionTitle: String = ok, baseViewController: UIViewController? = nil, tintColor: UIColor? = nil, closure: CompletionHandler?) {
alert(title, message: message, actionTitle: actionTitle, addCancelAction: true, baseViewController: baseViewController, tintColor: tintColor, closure: closure)
}
class func configuredWithCancelAction(_ title: String = "", message: String = "", actionTitle: String = ok, tintColor: UIColor? = nil, closure: CompletionHandler?) -> UIAlertController {
return configured(title, message: message, actionTitle: actionTitle, addCancelAction: true, tintColor: tintColor, closure: closure)
}
class func multiAlertsWithOptions(multiItemsOfInfo: [String], baseViewController: UIViewController? = nil, tintColor: UIColor? = nil, closure: CompletionHandler?) {
alertWithOptions(multiItemsOfInfo, baseViewController: baseViewController, tintColor: tintColor, closure: closure)
}
fileprivate class func alert(_ title: String = "", message: String = "", actionTitle: String = ok, addCancelAction: Bool, baseViewController: UIViewController? = nil, tintColor: UIColor? = nil, closure: CompletionHandler?) {
guard let baseVC = baseViewController ?? UIApplication.shared.topMostViewController else { return }
let alertController = configured(title, message: message, actionTitle: actionTitle, addCancelAction: addCancelAction, tintColor: tintColor, closure: closure)
baseVC.present(alertController, animated: true, completion: nil)
}
fileprivate class func configured(_ title: String = "", message: String = "", actionTitle: String = ok, addCancelAction: Bool, tintColor: UIColor? = nil, closure: CompletionHandler?) -> UIAlertController {
let okAction = UIAlertAction(title: actionTitle, style: .default) { (_) -> Void in
closure?(true)
}
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
if addCancelAction {
let cancelAction = UIAlertAction(title: cancel, style: .cancel) { (_) -> Void in
closure?(false)
}
alertController.addAction(cancelAction)
}
alertController.addAction(okAction)
tintColor != nil ? alertController.view.tintColor = tintColor : ()
return alertController
}
fileprivate class func alertWithOptions(_ multiItemsOfInfo: [String], baseViewController: UIViewController? = nil, tintColor: UIColor? = nil, closure: CompletionHandler?) {
guard let baseVC = baseViewController ?? UIApplication.shared.topMostViewController else { return }
GlobalMainQueue.async {
var tempInfoArray = multiItemsOfInfo
let cancelAction = UIAlertAction(title: cancel, style: .cancel, handler: { (_) -> Void in
tempInfoArray.removeAll()
closure?(false)
})
let okAction = UIAlertAction(title: ok, style: .default, handler: { (_) -> Void in
tempInfoArray.removeFirst()
if tempInfoArray.count == 0 {
closure?(true)
return
}
self.alertWithOptions(tempInfoArray, closure: closure)
})
guard tempInfoArray.count > 0 else { return }
let alertController = UIAlertController(title: nil, message: tempInfoArray.first, preferredStyle: .alert)
tintColor != nil ? alertController.view.tintColor = tintColor : ()
alertController.addAction(cancelAction)
alertController.addAction(okAction)
baseVC.present(alertController, animated: true, completion: nil)
}
}
}