From d9292c3fc75c3ca85487163f330e39d8ede521e7 Mon Sep 17 00:00:00 2001 From: wengki81 Date: Mon, 12 Jan 2026 08:48:07 +0800 Subject: [PATCH] 20260112-01 --- example-app/ios/App/App/Info.plist | 6 -- .../DumonGeolocation.swift | 57 +++---------------- .../DumonGeolocationPlugin.swift | 24 +++++--- package.json | 2 +- src/definitions.ts | 2 +- 5 files changed, 25 insertions(+), 66 deletions(-) diff --git a/example-app/ios/App/App/Info.plist b/example-app/ios/App/App/Info.plist index 5ae6701..a864e25 100644 --- a/example-app/ios/App/App/Info.plist +++ b/example-app/ios/App/App/Info.plist @@ -47,11 +47,5 @@ NSLocationWhenInUseUsageDescription We use your location to provide real-time positioning. - NSLocationAlwaysAndWhenInUseUsageDescription - We use your location to provide real-time positioning, even in the background. - UIBackgroundModes - - location - diff --git a/ios/Sources/DumonGeolocationPlugin/DumonGeolocation.swift b/ios/Sources/DumonGeolocationPlugin/DumonGeolocation.swift index ac78d9b..2d62bf8 100644 --- a/ios/Sources/DumonGeolocationPlugin/DumonGeolocation.swift +++ b/ios/Sources/DumonGeolocationPlugin/DumonGeolocation.swift @@ -161,15 +161,8 @@ import UIKit } public func startBackgroundTracking() { - backgroundTrackingActive = true - if #available(iOS 9.0, *) { - locationManager.allowsBackgroundLocationUpdates = true - } - if #available(iOS 11.0, *) { - locationManager.showsBackgroundLocationIndicator = true - } - locationManager.pausesLocationUpdatesAutomatically = false - locationManager.startUpdatingLocation() + // Background tracking disabled on iOS to avoid background location reporting. + backgroundTrackingActive = false } public func stopBackgroundTracking() { @@ -184,7 +177,12 @@ import UIKit } public func isBackgroundTrackingActive() -> Bool { - return backgroundTrackingActive + // iOS only tracks in foreground; expose active tracking state for API parity. + return backgroundTrackingActive || foregroundTrackingActive + } + + public func isForegroundTrackingActive() -> Bool { + return foregroundTrackingActive } public func setAuthTokens(accessToken: String, refreshToken: String) { @@ -225,10 +223,6 @@ import UIKit latestTimestampMs = location.timestamp.timeIntervalSince1970 * 1000.0 backgroundLatestLocation = location - if backgroundTrackingActive { - postFixIfNeeded(location) - } - emitPositionUpdate(from: location, forceEmit: false) } @@ -421,39 +415,4 @@ import UIKit } } - private func postFixIfNeeded(_ location: CLLocation) { - guard let urlString = getBackgroundPostUrl(), let url = URL(string: urlString) else { return } - - let nowMs = Date().timeIntervalSince1970 * 1000.0 - if nowMs - lastPostAttemptMs < backgroundMinPostIntervalMs { - return - } - - if location.horizontalAccuracy > backgroundPostMinAccuracyMeters { - return - } - - if let lastPosted = lastPostedLocation { - let dist = location.distance(from: lastPosted) - if dist < backgroundPostMinDistanceMeters { - return - } - } - - lastPostAttemptMs = nowMs - let payload = buildPositionData(from: location) - - var request = URLRequest(url: url) - request.httpMethod = "POST" - request.setValue("application/json", forHTTPHeaderField: "Content-Type") - if let token = defaults.string(forKey: accessTokenKey) { - request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") - } - - request.httpBody = try? JSONSerialization.data(withJSONObject: payload, options: []) - - URLSession.shared.dataTask(with: request) { [weak self] _, _, _ in - self?.lastPostedLocation = location - }.resume() - } } diff --git a/ios/Sources/DumonGeolocationPlugin/DumonGeolocationPlugin.swift b/ios/Sources/DumonGeolocationPlugin/DumonGeolocationPlugin.swift index adc1cb6..59b3948 100644 --- a/ios/Sources/DumonGeolocationPlugin/DumonGeolocationPlugin.swift +++ b/ios/Sources/DumonGeolocationPlugin/DumonGeolocationPlugin.swift @@ -33,6 +33,7 @@ public class DumonGeolocationPlugin: CAPPlugin, CAPBridgedPlugin { private let implementation = DumonGeolocation() private var pendingPermissionCall: CAPPluginCall? private var pendingBackgroundCall: CAPPluginCall? + private var backgroundMappedToForeground = false public override func load() { implementation.onPositionUpdate = { [weak self] data in @@ -99,21 +100,26 @@ public class DumonGeolocationPlugin: CAPPlugin, CAPBridgedPlugin { } @objc func startBackgroundTracking(_ call: CAPPluginCall) { - if let postUrl = call.getString("postUrl") { - implementation.setBackgroundPostUrl(postUrl) - } + // iOS only supports foreground tracking; reuse foreground flow. let status = implementation.authorizationStatus() - if status == .authorizedAlways { - implementation.startBackgroundTracking() - call.resolve() + if !isLocationAuthorized(status) { + call.reject("Location permission not granted") return } - pendingBackgroundCall = call - implementation.requestAlwaysPermission() + if !implementation.isForegroundTrackingActive() { + implementation.startPositioning() + backgroundMappedToForeground = true + } else { + backgroundMappedToForeground = false + } + call.resolve() } @objc func stopBackgroundTracking(_ call: CAPPluginCall) { - implementation.stopBackgroundTracking() + if backgroundMappedToForeground && implementation.isForegroundTrackingActive() { + implementation.stopPositioning() + } + backgroundMappedToForeground = false call.resolve() } diff --git a/package.json b/package.json index 521b320..9c8e188 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dumon-geolocation", - "version": "1.1.1", + "version": "1.1.2", "description": "Implement manager GNSS, Wi‑Fi RTT, IMU, Kalman fusion, event emitter", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", diff --git a/src/definitions.ts b/src/definitions.ts index 3efe297..14601b1 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -110,7 +110,7 @@ export interface DumonGeolocationPlugin { setOptions(options: DumonGeoOptions): Promise; getGnssStatus(): Promise; getLocationServicesStatus(): Promise<{ gpsEnabled: boolean; networkEnabled: boolean }>; - // Background tracking (Android) + // Background tracking (Android; iOS maps to foreground tracking only) startBackgroundTracking(options?: { title?: string; text?: string;