@@ -98,6 +98,7 @@ private class LocationManager: CLLocationManager, CLLocationManagerDelegate {
98
98
99
99
extension CLLocationManager {
100
100
/// request CoreLocation authorization from user
101
+ /// NOTE if you want to do whenInUse -> always upgrades then you must specify the auth-type yourself.
101
102
@available ( iOS 8 , * )
102
103
public class func requestAuthorization( type: RequestAuthorizationType = . automatic) -> Guarantee < CLAuthorizationStatus > {
103
104
return AuthorizationCatcher ( auther: auther ( type) , type: type) . promise
@@ -108,25 +109,30 @@ extension CLLocationManager {
108
109
private class AuthorizationCatcher : CLLocationManager , CLLocationManagerDelegate {
109
110
let ( promise, fulfill) = Guarantee< CLAuthorizationStatus> . pending( )
110
111
var retainCycle : AuthorizationCatcher ?
112
+ let initialAuthorizationState = CLLocationManager . authorizationStatus ( )
111
113
112
114
init ( auther: ( CLLocationManager ) -> Void , type: CLLocationManager . RequestAuthorizationType ) {
113
115
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, _) :
117
122
delegate = self
118
123
auther ( self )
119
124
retainCycle = self
120
125
default :
121
- fulfill ( status )
126
+ fulfill ( initialAuthorizationState )
122
127
}
123
128
promise. done { _ in
124
129
self . retainCycle = nil
125
130
}
126
131
}
127
132
128
133
@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 {
130
136
fulfill ( status)
131
137
}
132
138
}
@@ -143,12 +149,12 @@ private func auther(_ requestAuthorizationType: CLLocationManager.RequestAuthori
143
149
144
150
switch requestAuthorizationType {
145
151
case . automatic:
146
- let always = hasInfoPlistKey ( " NSLocationAlwaysUsageDescription " ) || hasInfoPlistKey ( " NSLocationAlwaysAndWhenInUsageDescription " )
152
+ let always = hasInfoPlistKey ( " NSLocationAlwaysUsageDescription " ) || hasInfoPlistKey ( " NSLocationAlwaysAndWhenInUseUsageDescription " )
147
153
let whenInUse = { hasInfoPlistKey ( " NSLocationWhenInUseUsageDescription " ) }
148
154
if always {
149
155
manager. requestAlwaysAuthorization ( )
150
156
} else {
151
- if !whenInUse( ) { NSLog ( " PromiseKit: Warning: `NSLocationWhenInUseUsageDescription ` key not set " ) }
157
+ if !whenInUse( ) { NSLog ( " PromiseKit: Warning: `NSLocationAlwaysAndWhenInUseUsageDescription ` key not set " ) }
152
158
manager. requestWhenInUseAuthorization ( )
153
159
}
154
160
case . whenInUse:
0 commit comments