-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathViewController.swift
252 lines (162 loc) · 8.93 KB
/
ViewController.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// ViewController.swift
// Managed View
//
import UIKit
class ViewController: UIViewController {
// let UserDefaults: Foundation.UserDefaults = Foundation.UserDefaults.standard
@IBOutlet var WebView: UIWebView!
// Default URL to display in web view
var defaultURL = URL(string: "http://maximlink.com/readme")
// Maintenance mode status
var MAINTENANCE_MODE = "OFF"
// Last URL loaded in web view
var url = URL(string: "")
// Pending URL to load
var newurl = URL(string: "")
// Autonomous Single App Mode (Mode)
var asamStatus:Bool = true
var asamStatusString:String = ""
override func viewDidLoad() {
super.viewDidLoad()
// request URL to web view
setupView()
// observe if App Config pushed from MDM
NotificationCenter.default.addObserver(forName: UserDefaults.didChangeNotification, object: nil, queue: OperationQueue.main) { _ in
self.setupView()
print ("reload")
}
}
// Tap Gesture Recognizer (triple tap defined in storyboard)
// Gesture used as interactive method to enable or disable Autonomous Single App Mode
@IBAction func tripleTap(_ sender: AnyObject) {
// If ASAM is enabled
if (UIAccessibilityIsGuidedAccessEnabled() == true ) {
asamStatus = true
asamStatusString = "ENABLED"
}
// if ASAM is not enabled
else {
asamStatus = false
asamStatusString = "DISABLED"
}
print (asamStatus)
// define dialog to user
let actionSheetController: UIAlertController = UIAlertController(title: "Autonomous Single App Mode is currently\n \(asamStatusString)", message: "Select action", preferredStyle: .actionSheet)
// Customize user dialog based on current state of ASAM and reguest ASAM state change
if (asamStatus == false) {
let asamEnable: UIAlertAction = UIAlertAction(title: "Enable", style: .default) { action -> Void in
UIAccessibilityRequestGuidedAccessSession(true) {
success in
print("INFO: ASAM request to set ON")
if success {
print ("ASAM is enabled")
let asamAlert = UIAlertController(title: "Success", message: "Autonomous Single App Mode is\n\n ENABLED.", preferredStyle: UIAlertControllerStyle.alert)
asamAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(asamAlert, animated: true, completion: nil)
}
else {
print ("INFO: ASAM is not capable.")
let asamAlert = UIAlertController(title: "Autonomous Single App Mode is not supported", message: "This device does not currently support Automonous Single App Mode (ASAM). ASAM requires the following:\n\n (1) Device is in supervised state.\n\n(2) Configuration profile supporting ASAM for this specific app installed on device.", preferredStyle: UIAlertControllerStyle.alert)
asamAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(asamAlert, animated: true, completion: nil)
}
}
}
actionSheetController.addAction(asamEnable)
}
else {
let asamDisable: UIAlertAction = UIAlertAction(title: "Disable", style: .default) { action -> Void in
UIAccessibilityRequestGuidedAccessSession(false) {
success in
print("INFO: ASAM request to set OFF")
if success {
print ("ASAM is disenabled")
let asamAlert = UIAlertController(title: "Success", message: "Autonomous Single App Mode is\n\n DISABLED.", preferredStyle: UIAlertControllerStyle.alert)
asamAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(asamAlert, animated: true, completion: nil)
}
else {
print ("INFO: ASAM is not capable.")
let asamAlert = UIAlertController(title: "Autonomous Single App Mode is not supported", message: "This device does not currently support Automonous Single App Mode (ASAM). ASAM requires the following:\n\n (1) Device is in supervised state.\n\n(2) Configuration profile supporting ASAM for this specific app installed on device.", preferredStyle: UIAlertControllerStyle.alert)
asamAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(asamAlert, animated: true, completion: nil)
}
}
}
actionSheetController.addAction(asamDisable)
}
//Create and add the Cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .destructive) { action -> Void in
//Just dismiss the action sheet
}
actionSheetController.addAction(cancelAction)
// for iPad
actionSheetController.popoverPresentationController?.sourceView = view
// Present dialog to user
self.present(actionSheetController, animated: true, completion: nil)
}
func setupView() {
print ("INFO: setupView")
// Check for Manged App Config
if let ManAppConfig = UserDefaults.standard.dictionary(forKey: "com.apple.configuration.managed") {
// Check if MAINTENANCE_MODE key is set
if (ManAppConfig["MAINTENANCE_MODE"] != nil) {
MAINTENANCE_MODE = String(describing: ManAppConfig["MAINTENANCE_MODE"]!)
}
else {
MAINTENANCE_MODE = "OFF"
}
// Check if MAINTENANCE_MODE key is set to "ON"
if (String(describing: ManAppConfig["MAINTENANCE_MODE"]!) == "ON") {
newurl = URL.init(fileURLWithPath: Bundle.main.path(forResource: "curtain", ofType: "png", inDirectory: "img")!)
// If URL changed since last web view load then load new URL
if newurl != url {
print ("STATUS: loading maintenacne URL \(newurl!)")
let request = URLRequest(url: newurl!)
WebView.loadRequest(request)
url = newurl
}
}
else {
// Check if URL key is set
if (ManAppConfig["URL"] != nil) {
newurl = URL(string: String(describing: ManAppConfig["URL"]!))
// If URL changed since last web view load then load new URL
if newurl != url {
print ("STATUS: loading updated AppConfig URL \(newurl!)")
let request = URLRequest(url: newurl!)
WebView.loadRequest(request)
url = newurl
}
}
// If no Manged App Config URL key set then use default URL
else {
newurl = defaultURL
// If URL changed since last web view load then load new URL
if newurl != url {
print ("STATUS: loading default URL \(newurl!)")
let request = URLRequest(url: newurl!)
WebView.loadRequest(request)
url = newurl
}
}
}
}
// If no Manged App Config then use default URL
else {
newurl = defaultURL
// If URL changed since last web view load then load new URL
if newurl != url {
print ("INFO: Refreshing2 \(newurl!)")
let request = URLRequest(url: newurl!)
WebView.loadRequest(request)
url = newurl
}
}
}
// Hide Top Status Bar
override var prefersStatusBarHidden : Bool {
return true
}
}