Skip to content

Commit

Permalink
fix(ios): Don't get location if permission is not determined (#3802)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Nov 18, 2020
1 parent 249073d commit 4fb9d34
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions ios/Capacitor/Capacitor/Plugins/Geolocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 4fb9d34

Please sign in to comment.