@ -14,6 +14,7 @@ class GetPositionArgs: Decodable {
class WatchPositionArgs : Decodable {
let options : GetPositionArgs
let request_updates_in_background : Bool
let channel : Channel
}
@ -21,9 +22,20 @@ class ClearWatchArgs: Decodable {
let channelId : UInt32
}
enum PermissionType : String , Decodable {
case location
case coarseLocation
}
class RequestPermissionsArgs : Decodable {
let permissions : [ PermissionType ]
let requestUpdatesInBackground : Bool
}
class GeolocationPlugin : Plugin , CLLocationManagerDelegate {
private let locationManager = CLLocationManager ( )
private var isUpdatingLocation : Bool = false
private var backgroundUpdatesRequested : Bool = false
private var permissionRequests : [ Invoke ] = [ ]
private var positionRequests : [ Invoke ] = [ ]
private var watcherChannels : [ Channel ] = [ ]
@ -61,6 +73,8 @@ class GeolocationPlugin: Plugin, CLLocationManagerDelegate {
@objc public func watchPosition ( _ invoke : Invoke ) throws {
let args = try invoke . parseArgs ( WatchPositionArgs . self )
self . backgroundUpdatesRequested = args . request_updates_in_background ;
self . watcherChannels . append ( args . channel )
DispatchQueue . main . async {
@ -70,12 +84,28 @@ class GeolocationPlugin: Plugin, CLLocationManagerDelegate {
self . locationManager . desiredAccuracy = kCLLocationAccuracyKilometer
}
// TODO: U s e t h e a u t h o r i z a t i o n S t a t u s i n s t a n c e p r o p e r t y w i t h l o c a t i o n M a n a g e r D i d C h a n g e A u t h o r i z a t i o n ( _ : ) i n s t e a d .
if CLLocationManager . authorizationStatus ( ) = = . notDetermined {
if self . backgroundUpdatesRequested {
self . locationManager . requestAlwaysAuthorization ( )
} else {
self . locationManager . requestWhenInUseAuthorization ( )
}
} else {
if CLLocationManager . authorizationStatus ( ) = = . authorizedWhenInUse && self . backgroundUpdatesRequested {
self . locationManager . requestAlwaysAuthorization ( )
} else {
self . locationManager . startUpdatingLocation ( )
self . isUpdatingLocation = true
// E n a b l e b a c k g r o u n d u p d a t e s i f e n a b l e d
if self . backgroundUpdatesRequested {
self . locationManager . allowsBackgroundLocationUpdates = true
self . locationManager . pausesLocationUpdatesAutomatically = false
self . locationManager . showsBackgroundLocationIndicator = true
}
}
}
}
@ -125,9 +155,19 @@ class GeolocationPlugin: Plugin, CLLocationManagerDelegate {
// TODO: U s e t h e a u t h o r i z a t i o n S t a t u s i n s t a n c e p r o p e r t y w i t h l o c a t i o n M a n a g e r D i d C h a n g e A u t h o r i z a t i o n ( _ : ) i n s t e a d .
if CLLocationManager . authorizationStatus ( ) = = . notDetermined {
self . permissionRequests . append ( invoke )
do {
let args = try invoke . parseArgs ( RequestPermissionsArgs . self )
DispatchQueue . main . async {
self . locationManager . requestWhenInUseAuthorization ( )
if args . requestUpdatesInBackground {
// R e q u e s t b a c k g r o u n d p e r m i s s i o n i f r e q u e s t e d
self . locationManager . requestAlwaysAuthorization ( )
self . backgroundUpdatesRequested = true
}
}
} catch {
invoke . reject ( error . localizedDescription )
}
} else {
checkPermissions ( invoke )
@ -224,6 +264,11 @@ class GeolocationPlugin: Plugin, CLLocationManagerDelegate {
private func stopUpdating ( ) {
self . locationManager . stopUpdatingLocation ( )
self . isUpdatingLocation = false
if self . backgroundUpdatesRequested {
self . locationManager . allowsBackgroundLocationUpdates = false
self . locationManager . pausesLocationUpdatesAutomatically = true
self . locationManager . showsBackgroundLocationIndicator = false
}
}
private func convertLocation ( _ location : CLLocation ) -> JsonObject {