@@ -53,8 +53,8 @@ public class GeolocationPlugin: CAPPlugin, CAPBridgedPlugin {
5353 callbackManager? . sendSuccess ( call)
5454 }
5555
56- override public func checkPermissions( _ call: CAPPluginCall ) {
57- checkIfLocationServicesAreEnabled ( )
56+ @ objc override public func checkPermissions( _ call: CAPPluginCall ) {
57+ guard checkIfLocationServicesAreEnabled ( call ) else { return }
5858
5959 let status = switch locationService? . authorisationStatus {
6060 case . restricted, . denied: Constants . AuthorisationStatus. Status. denied
@@ -69,11 +69,12 @@ public class GeolocationPlugin: CAPPlugin, CAPBridgedPlugin {
6969 callbackManager? . sendSuccess ( call, with: callResultData)
7070 }
7171
72- override public func requestPermissions( _ call: CAPPluginCall ) {
73- checkIfLocationServicesAreEnabled ( )
72+ @ objc override public func requestPermissions( _ call: CAPPluginCall ) {
73+ guard checkIfLocationServicesAreEnabled ( call ) else { return }
7474
7575 if locationService? . authorisationStatus == . notDetermined {
7676 shouldSetupBindings ( )
77+ callbackManager? . addRequestPermissionsCallback ( capacitorCall: call)
7778 } else {
7879 checkPermissions ( call)
7980 }
@@ -94,13 +95,13 @@ private extension GeolocationPlugin {
9495
9596 switch status {
9697 case . denied:
97- self . callbackManager ? . sendError ( . permissionDenied)
98+ self . onLocationPermissionNotGranted ( error : . permissionDenied)
9899 case . notDetermined:
99100 self . requestLocationAuthorisation ( type: . whenInUse)
100101 case . restricted:
101- self . callbackManager ? . sendError ( . permissionRestricted)
102+ self . onLocationPermissionNotGranted ( error : . permissionRestricted)
102103 case . authorisedAlways, . authorisedWhenInUse:
103- self . requestLocation ( )
104+ self . onLocationPermissionGranted ( )
104105 @unknown default : break
105106 }
106107 } )
@@ -120,23 +121,41 @@ private extension GeolocationPlugin {
120121
121122 func requestLocationAuthorisation( type requestType: IONGLOCAuthorisationRequestType ) {
122123 DispatchQueue . global ( qos: . background) . async {
123- self . checkIfLocationServicesAreEnabled ( )
124+ guard self . checkIfLocationServicesAreEnabled ( ) else { return }
124125 self . locationService? . requestAuthorisation ( withType: requestType)
125126 }
126127 }
127128
128- func checkIfLocationServicesAreEnabled( ) {
129- guard locationService? . areLocationServicesEnabled ( ) ?? false else {
130- callbackManager? . sendError ( . locationServicesDisabled)
131- return
129+ func checkIfLocationServicesAreEnabled( _ call: CAPPluginCall ? = nil ) -> Bool {
130+ guard locationService? . areLocationServicesEnabled ( ) == true else {
131+ call. map { callbackManager? . sendError ( $0, error: . locationServicesDisabled) }
132+ ?? callbackManager? . sendError ( . locationServicesDisabled)
133+ return false
134+ }
135+ return true
136+ }
137+
138+ func onLocationPermissionNotGranted( error: GeolocationError ) {
139+ let shouldNotifyRequestPermissionsResult = callbackManager? . requestPermissionsCallbacks. isEmpty == false
140+ let shouldNotifyPermissionError = callbackManager? . locationCallbacks. isEmpty == false || callbackManager? . watchCallbacks. isEmpty == false
141+
142+ if shouldNotifyRequestPermissionsResult {
143+ self . callbackManager? . sendRequestPermissionsSuccess ( Constants . AuthorisationStatus. Status. denied)
144+ }
145+ if shouldNotifyPermissionError {
146+ self . callbackManager? . sendError ( error)
132147 }
133148 }
134149
135- func requestLocation( ) {
136- // should request if callbacks exist and are not empty
150+ func onLocationPermissionGranted( ) {
151+ let shouldNotifyPermissionGranted = callbackManager? . requestPermissionsCallbacks. isEmpty == false
152+ // should request location if callbacks below exist and are not empty
137153 let shouldRequestCurrentPosition = callbackManager? . locationCallbacks. isEmpty == false
138154 let shouldRequestLocationMonitoring = callbackManager? . watchCallbacks. isEmpty == false
139155
156+ if shouldNotifyPermissionGranted {
157+ callbackManager? . sendRequestPermissionsSuccess ( Constants . AuthorisationStatus. Status. granted)
158+ }
140159 if shouldRequestCurrentPosition {
141160 locationService? . requestSingleLocation ( )
142161 }
@@ -156,7 +175,7 @@ private extension GeolocationPlugin {
156175 }
157176
158177 switch locationService? . authorisationStatus {
159- case . authorisedAlways, . authorisedWhenInUse: requestLocation ( )
178+ case . authorisedAlways, . authorisedWhenInUse: onLocationPermissionGranted ( )
160179 case . denied: callbackManager? . sendError ( . permissionDenied)
161180 case . restricted: callbackManager? . sendError ( . permissionRestricted)
162181 default : break
0 commit comments