Skip to content

Commit 8f4aec5

Browse files
committed
Allow whenInUse -> always upgrades;
Fixes #4; Closes #5.
1 parent 5a0535a commit 8f4aec5

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Sources/CLLocationManager+Promise.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ private class LocationManager: CLLocationManager, CLLocationManagerDelegate {
9898

9999
extension CLLocationManager {
100100
/// request CoreLocation authorization from user
101+
/// NOTE if you want to do whenInUse -> always upgrades then you must specify the auth-type yourself.
101102
@available(iOS 8, *)
102103
public class func requestAuthorization(type: RequestAuthorizationType = .automatic) -> Guarantee<CLAuthorizationStatus> {
103104
return AuthorizationCatcher(auther: auther(type), type: type).promise
@@ -108,25 +109,30 @@ extension CLLocationManager {
108109
private class AuthorizationCatcher: CLLocationManager, CLLocationManagerDelegate {
109110
let (promise, fulfill) = Guarantee<CLAuthorizationStatus>.pending()
110111
var retainCycle: AuthorizationCatcher?
112+
let initialAuthorizationState = CLLocationManager.authorizationStatus()
111113

112114
init(auther: (CLLocationManager) -> Void, type: CLLocationManager.RequestAuthorizationType) {
113115
super.init()
114-
let status = CLLocationManager.authorizationStatus()
115-
switch (status, type) {
116-
case (.notDetermined, _), (.authorizedWhenInUse, .always), (.authorizedWhenInUse, .automatic):
116+
switch (initialAuthorizationState, type) {
117+
case (.authorizedWhenInUse, .always), (.authorizedWhenInUse, .automatic):
118+
if #available(iOS 11.0, tvOS 100.0, watchOS 100.0, macOS 100.0, *) {
119+
fallthrough
120+
}
121+
case (.notDetermined, _):
117122
delegate = self
118123
auther(self)
119124
retainCycle = self
120125
default:
121-
fulfill(status)
126+
fulfill(initialAuthorizationState)
122127
}
123128
promise.done { _ in
124129
self.retainCycle = nil
125130
}
126131
}
127132

128133
@objc fileprivate func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
129-
if status != .notDetermined {
134+
// `didChange` is a lie; it fires this immediately with the current status.
135+
if status != initialAuthorizationState {
130136
fulfill(status)
131137
}
132138
}
@@ -143,12 +149,12 @@ private func auther(_ requestAuthorizationType: CLLocationManager.RequestAuthori
143149

144150
switch requestAuthorizationType {
145151
case .automatic:
146-
let always = hasInfoPlistKey("NSLocationAlwaysUsageDescription") || hasInfoPlistKey("NSLocationAlwaysAndWhenInUsageDescription")
152+
let always = hasInfoPlistKey("NSLocationAlwaysUsageDescription") || hasInfoPlistKey("NSLocationAlwaysAndWhenInUseUsageDescription")
147153
let whenInUse = { hasInfoPlistKey("NSLocationWhenInUseUsageDescription") }
148154
if always {
149155
manager.requestAlwaysAuthorization()
150156
} else {
151-
if !whenInUse() { NSLog("PromiseKit: Warning: `NSLocationWhenInUseUsageDescription` key not set") }
157+
if !whenInUse() { NSLog("PromiseKit: Warning: `NSLocationAlwaysAndWhenInUseUsageDescription` key not set") }
152158
manager.requestWhenInUseAuthorization()
153159
}
154160
case .whenInUse:

0 commit comments

Comments
 (0)