-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLocationLibrary.swift
51 lines (44 loc) · 1.33 KB
/
LocationLibrary.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
//
// LocationLibrarh.swift
// IFTTT SDK
//
// Copyright © 2020 IFTTT. All rights reserved.
//
import Foundation
import CoreLocation
final class LocationLibrary: NSObject, Library, CLLocationManagerDelegate {
var access: LibraryAccess {
switch CLLocationManager.authorizationStatus() {
case .authorizedAlways, .authorizedWhenInUse:
return .authorized
case .notDetermined:
return .notDetermined
case .restricted:
return .restricted
case .denied:
return .denied
@unknown default:
return .notDetermined
}
}
private let locationManager: CLLocationManager
private var completion: ((LibraryAccess) -> Void)?
init(locationManager: CLLocationManager) {
self.locationManager = locationManager
super.init()
locationManager.delegate = self
}
func requestAccess(_ completion: @escaping (LibraryAccess) -> Void) {
if access != .authorized {
locationManager.requestAlwaysAuthorization()
} else {
completion(access)
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
completion?(access)
}
deinit {
locationManager.delegate = nil
}
}