From 4fb9d348d8805344c0c020c1cbb868d5bb6405cb Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Wed, 18 Nov 2020 18:19:19 +0100 Subject: [PATCH] fix(ios): Don't get location if permission is not determined (#3802) --- .../Capacitor/Plugins/Geolocation.swift | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/ios/Capacitor/Capacitor/Plugins/Geolocation.swift b/ios/Capacitor/Capacitor/Plugins/Geolocation.swift index 8f384ad41..fde3a1bd6 100644 --- a/ios/Capacitor/Capacitor/Plugins/Geolocation.swift +++ b/ios/Capacitor/Capacitor/Plugins/Geolocation.swift @@ -14,16 +14,16 @@ public struct GeolocationCoords { class GetLocationHandler: NSObject, CLLocationManagerDelegate { var locationManager = CLLocationManager() var call: CAPPluginCall + var shouldWatch: Bool + var hasPendingOperation: Bool init(call: CAPPluginCall, options: [String:Any]) { self.call = call - + self.shouldWatch = options["watch"] as! Bool + self.hasPendingOperation = true super.init() - // TODO: Allow user to configure accuracy, request/authorization mode self.locationManager.delegate = self - self.locationManager.requestWhenInUseAuthorization() - let shouldWatch = options["watch"] as! Bool if call.getBool("enableHighAccuracy", false)! { if shouldWatch { self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation @@ -34,17 +34,26 @@ class GetLocationHandler: NSObject, CLLocationManagerDelegate { self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers } - if shouldWatch { - self.locationManager.startUpdatingLocation() + if CLLocationManager.authorizationStatus() == .notDetermined { + self.locationManager.requestWhenInUseAuthorization() } else { - self.locationManager.requestLocation() + getLocation(); } } public func stopUpdating() { self.locationManager.stopUpdatingLocation() } - + + public func getLocation() { + hasPendingOperation = false + if shouldWatch { + self.locationManager.startUpdatingLocation() + } else { + self.locationManager.requestLocation() + } + } + func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { call.error(error.localizedDescription, error, [ "message": error.localizedDescription @@ -61,7 +70,13 @@ class GetLocationHandler: NSObject, CLLocationManagerDelegate { call.success() } } - + + public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { + if status != .notDetermined && hasPendingOperation { + getLocation() + } + } + func makePosition(_ location: CLLocation) -> JSObject { var ret = JSObject() var coords = JSObject()