@capgo/background-geolocation 8.0.1 → 8.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ios/Sources/CapgoBackgroundGeolocationPlugin/CapgoCapacitorBackgroundGeolocationPlugin.swift
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// swiftlint:disable file_length
|
|
1
2
|
import Capacitor
|
|
2
3
|
import Foundation
|
|
3
4
|
import UIKit
|
|
@@ -13,8 +14,8 @@ func formatLocation(_ location: CLLocation) -> PluginCallResultData {
|
|
|
13
14
|
// Prior to iOS 15, it was not possible to detect simulated locations.
|
|
14
15
|
// But in general, it is very difficult to simulate locations on iOS in
|
|
15
16
|
// production.
|
|
16
|
-
if location.sourceInformation
|
|
17
|
-
simulated =
|
|
17
|
+
if let sourceInfo = location.sourceInformation {
|
|
18
|
+
simulated = sourceInfo.isSimulatedBySoftware
|
|
18
19
|
}
|
|
19
20
|
}
|
|
20
21
|
return [
|
|
@@ -35,8 +36,9 @@ func formatLocation(_ location: CLLocation) -> PluginCallResultData {
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
@objc(BackgroundGeolocation)
|
|
39
|
+
// swiftlint:disable:next type_body_length
|
|
38
40
|
public class BackgroundGeolocation: CAPPlugin, CLLocationManagerDelegate, CAPBridgedPlugin {
|
|
39
|
-
private let pluginVersion: String = "8.0.
|
|
41
|
+
private let pluginVersion: String = "8.0.3"
|
|
40
42
|
public let identifier = "BackgroundGeolocationPlugin"
|
|
41
43
|
public let jsName = "BackgroundGeolocation"
|
|
42
44
|
public let pluginMethods: [CAPPluginMethod] = [
|
|
@@ -57,7 +59,7 @@ public class BackgroundGeolocation: CAPPlugin, CLLocationManagerDelegate, CAPBri
|
|
|
57
59
|
private var distanceThreshold: Double = 50.0 // Default distance threshold in meters
|
|
58
60
|
|
|
59
61
|
// Earth radius in meters for distance calculations
|
|
60
|
-
private static let
|
|
62
|
+
private static let earthRadiusMeters: Double = 6371000.0
|
|
61
63
|
|
|
62
64
|
@objc override public func load() {
|
|
63
65
|
UIDevice.current.isBatteryMonitoringEnabled = true
|
|
@@ -74,55 +76,70 @@ public class BackgroundGeolocation: CAPPlugin, CLLocationManagerDelegate, CAPBri
|
|
|
74
76
|
}
|
|
75
77
|
// Create fresh location manager and initialize date
|
|
76
78
|
self.locationManager = CLLocationManager()
|
|
77
|
-
self.locationManager
|
|
79
|
+
guard let manager = self.locationManager else {
|
|
80
|
+
return call.reject("Failed to create location manager")
|
|
81
|
+
}
|
|
82
|
+
manager.delegate = self
|
|
78
83
|
self.created = Date()
|
|
79
84
|
|
|
80
85
|
let background = call.getString("backgroundMessage") != nil
|
|
81
86
|
self.allowStale = call.getBool("stale") ?? false
|
|
82
87
|
self.activeCallbackId = call.callbackId
|
|
83
88
|
|
|
84
|
-
|
|
85
|
-
.full,
|
|
86
|
-
.charging
|
|
87
|
-
].contains(UIDevice.current.batteryState)
|
|
88
|
-
self.locationManager!.desiredAccuracy = (
|
|
89
|
-
externalPower
|
|
90
|
-
? kCLLocationAccuracyBestForNavigation
|
|
91
|
-
: kCLLocationAccuracyBest
|
|
92
|
-
)
|
|
93
|
-
var distanceFilter = call.getDouble("distanceFilter")
|
|
94
|
-
// It appears that setting manager.distanceFilter to 0 can prevent
|
|
95
|
-
// subsequent location updates. See issue #88.
|
|
96
|
-
if distanceFilter == nil || distanceFilter == 0 {
|
|
97
|
-
distanceFilter = kCLDistanceFilterNone
|
|
98
|
-
}
|
|
99
|
-
self.locationManager!.distanceFilter = distanceFilter!
|
|
100
|
-
self.locationManager!.allowsBackgroundLocationUpdates = background
|
|
101
|
-
self.locationManager!.showsBackgroundLocationIndicator = background
|
|
102
|
-
self.locationManager!.pausesLocationUpdatesAutomatically = false
|
|
89
|
+
self.configureLocationManager(manager, call: call, background: background)
|
|
103
90
|
|
|
104
91
|
if call.getBool("requestPermissions") != false {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
.notDetermined,
|
|
108
|
-
.denied,
|
|
109
|
-
.restricted
|
|
110
|
-
].contains(status) {
|
|
111
|
-
return (
|
|
112
|
-
background
|
|
113
|
-
? self.locationManager!.requestAlwaysAuthorization()
|
|
114
|
-
: self.locationManager!.requestWhenInUseAuthorization()
|
|
115
|
-
)
|
|
116
|
-
}
|
|
117
|
-
if background && status == .authorizedWhenInUse {
|
|
118
|
-
// Attempt to escalate.
|
|
119
|
-
self.locationManager!.requestAlwaysAuthorization()
|
|
92
|
+
if self.handlePermissions(manager, background: background) {
|
|
93
|
+
return
|
|
120
94
|
}
|
|
121
95
|
}
|
|
122
96
|
return self.startUpdatingLocation()
|
|
123
97
|
}
|
|
124
98
|
}
|
|
125
99
|
|
|
100
|
+
private func configureLocationManager(_ manager: CLLocationManager, call: CAPPluginCall, background: Bool) {
|
|
101
|
+
let externalPower = [
|
|
102
|
+
.full,
|
|
103
|
+
.charging
|
|
104
|
+
].contains(UIDevice.current.batteryState)
|
|
105
|
+
manager.desiredAccuracy = (
|
|
106
|
+
externalPower
|
|
107
|
+
? kCLLocationAccuracyBestForNavigation
|
|
108
|
+
: kCLLocationAccuracyBest
|
|
109
|
+
)
|
|
110
|
+
var distanceFilter = call.getDouble("distanceFilter")
|
|
111
|
+
// It appears that setting manager.distanceFilter to 0 can prevent
|
|
112
|
+
// subsequent location updates. See issue #88.
|
|
113
|
+
if distanceFilter == nil || distanceFilter == 0 {
|
|
114
|
+
distanceFilter = kCLDistanceFilterNone
|
|
115
|
+
}
|
|
116
|
+
manager.distanceFilter = distanceFilter ?? kCLDistanceFilterNone
|
|
117
|
+
manager.allowsBackgroundLocationUpdates = background
|
|
118
|
+
manager.showsBackgroundLocationIndicator = background
|
|
119
|
+
manager.pausesLocationUpdatesAutomatically = false
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private func handlePermissions(_ manager: CLLocationManager, background: Bool) -> Bool {
|
|
123
|
+
let status = CLLocationManager.authorizationStatus()
|
|
124
|
+
if [
|
|
125
|
+
.notDetermined,
|
|
126
|
+
.denied,
|
|
127
|
+
.restricted
|
|
128
|
+
].contains(status) {
|
|
129
|
+
if background {
|
|
130
|
+
manager.requestAlwaysAuthorization()
|
|
131
|
+
} else {
|
|
132
|
+
manager.requestWhenInUseAuthorization()
|
|
133
|
+
}
|
|
134
|
+
return true
|
|
135
|
+
}
|
|
136
|
+
if background && status == .authorizedWhenInUse {
|
|
137
|
+
// Attempt to escalate.
|
|
138
|
+
manager.requestAlwaysAuthorization()
|
|
139
|
+
}
|
|
140
|
+
return false
|
|
141
|
+
}
|
|
142
|
+
|
|
126
143
|
@objc func stop(_ call: CAPPluginCall) {
|
|
127
144
|
// CLLocationManager requires main thread
|
|
128
145
|
DispatchQueue.main.async {
|
|
@@ -151,8 +168,7 @@ public class BackgroundGeolocation: CAPPlugin, CLLocationManagerDelegate, CAPBri
|
|
|
151
168
|
}
|
|
152
169
|
|
|
153
170
|
if UIApplication.shared.canOpenURL(settingsUrl) {
|
|
154
|
-
UIApplication.shared.open(settingsUrl, completionHandler: {
|
|
155
|
-
(success) in
|
|
171
|
+
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
|
|
156
172
|
if success {
|
|
157
173
|
return call.resolve()
|
|
158
174
|
} else {
|
|
@@ -252,7 +268,7 @@ public class BackgroundGeolocation: CAPPlugin, CLLocationManagerDelegate, CAPBri
|
|
|
252
268
|
|
|
253
269
|
let ccc = 2 * atan2(sqrt(aaa), sqrt(1 - aaa))
|
|
254
270
|
|
|
255
|
-
return BackgroundGeolocation.
|
|
271
|
+
return BackgroundGeolocation.earthRadiusMeters * ccc
|
|
256
272
|
}
|
|
257
273
|
|
|
258
274
|
private func distancePointToLineSegment(_ point: [Double], _ lineStart: [Double], _ lineEnd: [Double]) -> Double {
|
package/ios/Tests/CapgoBackgroundGeolocationPluginTests/CapgoBackgroundGeolocationPluginTests.swift
CHANGED
|
@@ -2,9 +2,10 @@ import XCTest
|
|
|
2
2
|
import CoreLocation
|
|
3
3
|
@testable import CapgoBackgroundGeolocationPlugin
|
|
4
4
|
|
|
5
|
+
// swiftlint:disable:next type_body_length
|
|
5
6
|
class CapgoBackgroundGeolocationTests: XCTestCase {
|
|
6
7
|
|
|
7
|
-
var plugin: BackgroundGeolocation
|
|
8
|
+
var plugin: BackgroundGeolocation?
|
|
8
9
|
|
|
9
10
|
override func setUp() {
|
|
10
11
|
super.setUp()
|
|
@@ -19,12 +20,20 @@ class CapgoBackgroundGeolocationTests: XCTestCase {
|
|
|
19
20
|
// MARK: - Plugin Initialization Tests
|
|
20
21
|
|
|
21
22
|
func testPluginInitialization() {
|
|
23
|
+
guard let plugin = plugin else {
|
|
24
|
+
XCTFail("Plugin should not be nil")
|
|
25
|
+
return
|
|
26
|
+
}
|
|
22
27
|
XCTAssertNotNil(plugin)
|
|
23
28
|
XCTAssertEqual(plugin.identifier, "BackgroundGeolocationPlugin")
|
|
24
29
|
XCTAssertEqual(plugin.jsName, "BackgroundGeolocation")
|
|
25
30
|
}
|
|
26
31
|
|
|
27
32
|
func testPluginMethods() {
|
|
33
|
+
guard let plugin = plugin else {
|
|
34
|
+
XCTFail("Plugin should not be nil")
|
|
35
|
+
return
|
|
36
|
+
}
|
|
28
37
|
let methodNames = plugin.pluginMethods.map { $0.name }
|
|
29
38
|
XCTAssertTrue(methodNames.contains("start"))
|
|
30
39
|
XCTAssertTrue(methodNames.contains("stop"))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capgo/background-geolocation",
|
|
3
|
-
"version": "8.0.
|
|
3
|
+
"version": "8.0.3",
|
|
4
4
|
"description": "Receive accurate geolocation updates even while the app is in the background.",
|
|
5
5
|
"main": "dist/plugin.cjs.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"bugs": {
|
|
25
25
|
"url": "https://github.com/Cap-go/capacitor-background-geolocation/issues"
|
|
26
26
|
},
|
|
27
|
+
"homepage": "https://capgo.app/docs/plugins/background-geolocation/",
|
|
27
28
|
"scripts": {
|
|
28
29
|
"verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
|
|
29
30
|
"verify:ios": "xcodebuild -scheme CapgoBackgroundGeolocation -destination generic/platform=iOS",
|