-
Notifications
You must be signed in to change notification settings - Fork 913
/
ViewController.swift
297 lines (266 loc) · 10.1 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import UIKit
import CoreLocation
class ManagerHolder {
let locman = CLLocationManager()
var doThisWhenAuthorized : (() -> ())?
func checkForLocationAccess(always: Bool=false, andThen f: (()->())?=nil) {
// no services? fail but try get alert
guard CLLocationManager.locationServicesEnabled() else {
print("no location services")
self.locman.startUpdatingLocation()
return
}
// new in iOS 14, use instance method, not class method
var status: CLAuthorizationStatus!
if #available(iOS 14.0,*) {
status = self.locman.authorizationStatus
} else {
status = CLLocationManager.authorizationStatus()
}
switch status {
case .authorizedWhenInUse:
if always { // try to step up
print("trying to step up")
self.doThisWhenAuthorized = f
self.locman.requestAlwaysAuthorization()
} else {
f?()
}
case .authorizedAlways:
f?()
case .notDetermined:
self.doThisWhenAuthorized = f
if always {
print("asking for always")
self.locman.requestAlwaysAuthorization()
} else {
print("asking for when in use")
self.locman.requestWhenInUseAuthorization()
}
case .restricted:
print("restricted")
break
case .denied:
print("denied")
break
default: fatalError()
}
}
}
class ViewController: UIViewController, CLLocationManagerDelegate {
var output = "" {
willSet {
// print(newValue)
}
didSet {
self.tv.text = output
self.tv.scrollRangeToVisible(NSMakeRange((self.tv.text as NSString).length-1,0))
}
}
@IBOutlet weak var tv: UITextView!
let managerHolder = ManagerHolder()
var locman : CLLocationManager {
return self.managerHolder.locman
}
required init?(coder: NSCoder) {
super.init(coder:coder)
self.locman.delegate = self
}
var startTime : Date!
var trying = false
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(goingIntoBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(comingIntoForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
return;
// for screen shots
let v = UIView()
v.backgroundColor = .white
v.frame = self.view.bounds
self.view.addSubview(v)
v.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.testWhenInUseRequest(self)
}
@objc func goingIntoBackground(_ n:Notification) {
let status : CLAuthorizationStatus = {
if #available(iOS 14.0,*) {
return self.locman.authorizationStatus
} else {
return CLLocationManager.authorizationStatus()
}
}()
if status == .authorizedWhenInUse {
self.locman.allowsBackgroundLocationUpdates = true // test background loc
}
if status == .authorizedAlways {
if CLLocationManager.significantLocationChangeMonitoringAvailable() {
print("start visit monitoring", to: &output)
self.locman.startMonitoringVisits() // test background monitoring
// self.locman.showsBackgroundLocationIndicator = true // test blue bar
} else {
print("no significant change monitoring, sorry", to: &output)
}
}
}
@objc func comingIntoForeground(_ n:Notification) {
self.locman.allowsBackgroundLocationUpdates = false
self.locman.stopMonitoringVisits()
}
// deprecated:
// func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
// if you implement both, the new one is called in iOS 14
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
let status : CLAuthorizationStatus = {
if #available(iOS 14.0,*) {
return self.locman.authorizationStatus
} else {
return CLLocationManager.authorizationStatus()
}
}()
print("authorization is", status.rawValue, to: &output)
if #available(iOS 14.0,*) {
print("accuracy is: \(manager.accuracyAuthorization.rawValue)", to: &output)
}
if status == .authorizedAlways || status == .authorizedWhenInUse {
self.managerHolder.doThisWhenAuthorized?()
}
self.managerHolder.doThisWhenAuthorized = nil
}
func locationManager(_ manager: CLLocationManager, didVisit visit: CLVisit) {
print(String(describing:visit), to:&output)
}
var which = 0
@IBAction func testWhenInUseRequest (_ sender: Any) {
self.managerHolder.checkForLocationAccess()
}
@IBAction func testAlwaysRequest (_ sender: Any) {
self.managerHolder.checkForLocationAccess(always:true)
}
@IBAction func testPrecisionRequest(_ sender: Any) {
if #available(iOS 14.0,*) {
self.locman.requestTemporaryFullAccuracyAuthorization(withPurposeKey: "ExactTracking") { err in
}
}
}
@IBAction func doClear(_ sender: Any) {
self.tv.text = ""
self.output = ""
}
@IBAction func doFindMe (_ sender: Any) {
self.managerHolder.checkForLocationAccess() {
self.which = 1
self.reallyFindMe()
}
}
@IBAction func whereAmI(_ sender: Any) {
self.managerHolder.checkForLocationAccess() {
self.which = 2
self.reallyFindMe()
}
}
@IBAction func reportStatus(_ sender: Any) {
let status : CLAuthorizationStatus = {
if #available(iOS 14.0,*) {
return self.locman.authorizationStatus
} else {
return CLLocationManager.authorizationStatus()
}
}()
print("auth is: \(status.rawValue)", to:&output)
if #available(iOS 14.0,*) {
print("accuracy is: \(self.locman.accuracyAuthorization.rawValue)", to: &output)
}
}
// NB new in iOS 14, kCLLocationAccuracyReduced is a thing
func reallyFindMe() {
switch self.which {
case 1:
if self.trying { return }
self.trying = true
self.locman.desiredAccuracy = kCLLocationAccuracyBest
self.locman.distanceFilter = kCLDistanceFilterNone
self.locman.activityType = .other
self.locman.pausesLocationUpdatesAutomatically = false
self.startTime = nil
print("starting", to:&self.output)
self.locman.startUpdatingLocation()
case 2:
print("requesting", to:&self.output)
self.locman.desiredAccuracy = kCLLocationAccuracyBest
self.locman.requestLocation()
default: break
}
}
func stopTrying () {
self.locman.stopUpdatingLocation()
self.locman.stopMonitoringVisits()
self.startTime = nil
self.trying = false
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("failed: \(error)", to:&output)
self.stopTrying()
}
@IBAction func stop(_ sender: Any) {
self.stopTrying()
}
let REQ_ACC : CLLocationAccuracy = 10
let REQ_TIME : TimeInterval = 30
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
switch which {
case 1:
print("did update location ", to:&output)
let loc = locations.last!
let acc = loc.horizontalAccuracy
let time = loc.timestamp
let coord = loc.coordinate
if self.startTime == nil {
self.startTime = Date()
// if reduced accuracy, don't ignore the first one, as the second one will be a long time coming!
if #available(iOS 14.0,*) {
if manager.accuracyAuthorization != .reducedAccuracy {
return // ignore first attempt
}
} else {
return // ignore first attempt
}
}
print("accuracy:", acc, to:&output)
let elapsed = time.timeIntervalSince(self.startTime)
if elapsed > REQ_TIME {
print("This is taking too long", to:&output)
print("You might be at \(coord.latitude) \(coord.longitude)", to:&output)
self.stopTrying()
return
}
if #available(iOS 14.0,*) {
if manager.accuracyAuthorization != .reducedAccuracy {
if acc < 0 || acc > REQ_ACC {
return // wait for the next one
}
}
} else {
if acc < 0 || acc > REQ_ACC {
return // wait for the next one
}
}
// got it
print("You are at \(coord.latitude) \(coord.longitude)", to:&output)
//self.stopTrying()
case 2:
let loc = locations.last!
let coord = loc.coordinate
print("The quick way: You are at \(coord.latitude) \(coord.longitude)", to:&output)
// bug: can be called twice in quick succession
// ok, the bug is gone; it seems that we just get the cached value the second time
// but these do keep arriving! is that a bug?
default: break
}
}
func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) {
print("paused!", to:&output)
}
func locationManagerDidResumeLocationUpdates(_ manager: CLLocationManager) {
print("resumed!", to:&output)
}
}