This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathLocation+OSX.swift
86 lines (69 loc) · 2.47 KB
/
Location+OSX.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
#if os(OSX)
import CoreLocation
import Foundation
import PSOperations
public struct Location: CapabilityType {
public static let name = "Location"
public init() { }
public func requestStatus(_ completion: @escaping (CapabilityStatus) -> Void) {
guard CLLocationManager.locationServicesEnabled() else {
completion(.notAvailable)
return
}
let actual = CLLocationManager.authorizationStatus()
switch actual {
case .notDetermined:
completion(.notDetermined)
case .restricted:
completion(.notAvailable)
case .denied:
completion(.denied)
case .authorized:
completion(.authorized)
default:
completion(.authorized)
}
}
public func authorize(_ completion: @escaping (CapabilityStatus) -> Void) {
Authorizer.authorize(completion: completion)
}
}
private let Authorizer = LocationAuthorizer()
private class LocationAuthorizer: NSObject, CLLocationManagerDelegate {
private let manager = CLLocationManager()
private var completion: ((CapabilityStatus) -> Void)?
override init() {
super.init()
manager.delegate = self
}
func authorize(completion: @escaping (CapabilityStatus) -> Void) {
guard self.completion == nil else {
fatalError("Attempting to authorize location when a request is already in-flight")
}
self.completion = completion
let key = "NSLocationUsageDescription"
manager.startUpdatingLocation()
// This is helpful when developing an app.
assert(Bundle.main.object(forInfoDictionaryKey: key) != nil, "Requesting location permission requires the \(key) key in your Info.plist")
}
@objc func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if let completion = self.completion, manager == self.manager {
self.completion = nil
switch status {
case .authorized:
completion(.authorized)
case .denied:
completion(.denied)
case .restricted:
completion(.notAvailable)
case .notDetermined:
self.completion = completion
manager.startUpdatingLocation()
manager.stopUpdatingLocation()
default:
completion(.authorized)
}
}
}
}
#endif