From 6c97bc87ddd0f649acdf86ae2cfb0638de6f4996 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Wed, 17 Feb 2021 11:22:10 +0100 Subject: [PATCH 01/21] Updated the iOS cleanup methods to make sure we have a clean state after the updates --- ios/Classes/CustomLogger.swift | 5 ++++ ...SwiftBackgroundLocationTrackerPlugin.swift | 27 +++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/ios/Classes/CustomLogger.swift b/ios/Classes/CustomLogger.swift index 656c8a8..a4c73bb 100644 --- a/ios/Classes/CustomLogger.swift +++ b/ios/Classes/CustomLogger.swift @@ -5,10 +5,15 @@ // Created by Dimmy Maenhout on 21/12/2020. // +import os import Foundation struct CustomLogger { static func log(message: String) { + if #available(iOS 10.0, *) { + let app = OSLog(subsystem: "com.waver.driver", category: "background tracker") + os_log("🔥 background-location log: %{public}@", log: app, type: .error, message) + } if SharedPrefsUtil.isLoggingEnabled() { print(message) } diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index daef252..65ee6aa 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -57,20 +57,17 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { let flutterCallbackInformation = FlutterCallbackCache.lookupCallbackInformation(callbackHandle) else { return } - var flutterEngine: FlutterEngine? = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) - flutterEngine!.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) - SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine!) + CustomLogger.log(message: "callbackHandle: \(callbackHandle)") + CustomLogger.log(message: "flutterCallbackInformation: \(flutterCallbackInformation)") + CustomLogger.log(message: "flutterCallbackInformation-name: \(String(describing: flutterCallbackInformation.callbackName))") + CustomLogger.log(message: "flutterCallbackInformation-callbackLibraryPath: \(String(describing: flutterCallbackInformation.callbackLibraryPath))") - var backgroundMethodChannel: FlutterMethodChannel? = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine!.binaryMessenger) + let flutterEngine: FlutterEngine = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) + flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) + SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) - - func cleanupFlutterResources() { - flutterEngine?.destroyContext() - backgroundMethodChannel = nil - flutterEngine = nil - } - - backgroundMethodChannel?.setMethodCallHandler { (call, result) in + let backgroundMethodChannel: FlutterMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) + backgroundMethodChannel.setMethodCallHandler { (call, result) in switch call.method { case BackgroundMethods.initialized.rawValue: result(true) @@ -79,12 +76,12 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { "lon": location.coordinate.longitude, "logging_enabled": SharedPrefsUtil.isLoggingEnabled(), ] - backgroundMethodChannel?.invokeMethod(BackgroundMethods.onLocationUpdate.rawValue, arguments: locationData, result: { flutterResult in - cleanupFlutterResources() + backgroundMethodChannel.invokeMethod(BackgroundMethods.onLocationUpdate.rawValue, arguments: locationData, result: { flutterResult in + flutterEngine.destroyContext() }) default: - cleanupFlutterResources() result(FlutterMethodNotImplemented) + flutterEngine.destroyContext() } } } From 3197d3105511754a9d70228d54cd738563a0a540 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Wed, 17 Feb 2021 15:36:18 +0100 Subject: [PATCH 02/21] Updated the iOS codebase to support iOS 11 + & added extra logs --- ios/Classes/CustomLogger.swift | 2 +- ios/Classes/LocationManager.swift | 3 + ...SwiftBackgroundLocationTrackerPlugin.swift | 74 ++++++++++--------- 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/ios/Classes/CustomLogger.swift b/ios/Classes/CustomLogger.swift index a4c73bb..61c07dd 100644 --- a/ios/Classes/CustomLogger.swift +++ b/ios/Classes/CustomLogger.swift @@ -11,7 +11,7 @@ struct CustomLogger { static func log(message: String) { if #available(iOS 10.0, *) { - let app = OSLog(subsystem: "com.waver.driver", category: "background tracker") + let app = OSLog(subsystem: "com.icapps.background_location_tracker", category: "background tracker") os_log("🔥 background-location log: %{public}@", log: app, type: .error, message) } if SharedPrefsUtil.isLoggingEnabled() { diff --git a/ios/Classes/LocationManager.swift b/ios/Classes/LocationManager.swift index 5728242..767375f 100644 --- a/ios/Classes/LocationManager.swift +++ b/ios/Classes/LocationManager.swift @@ -14,6 +14,9 @@ class LocationManager { let manager = CLLocationManager() manager.activityType = .automotiveNavigation manager.desiredAccuracy = kCLLocationAccuracyBest + if #available(iOS 11, *) { + manager.showsBackgroundLocationIndicator = true + } if #available(iOS 9.0, *) { manager.allowsBackgroundLocationUpdates = true } diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 65ee6aa..5183944 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -46,43 +46,47 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { private static let BACKGROUND_CHANNEL_NAME = "com.icapps.background_location_tracker/background_channel" public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { - guard let location = locations.last else { - CustomLogger.log(message: "No location ...") - return - } - - CustomLogger.log(message: "NEW LOCATION: \(location.coordinate.latitude): \(location.coordinate.longitude)") - - guard let callbackHandle = SharedPrefsUtil.getCallbackHandle(), - let flutterCallbackInformation = FlutterCallbackCache.lookupCallbackInformation(callbackHandle) - else { return } - - CustomLogger.log(message: "callbackHandle: \(callbackHandle)") - CustomLogger.log(message: "flutterCallbackInformation: \(flutterCallbackInformation)") - CustomLogger.log(message: "flutterCallbackInformation-name: \(String(describing: flutterCallbackInformation.callbackName))") - CustomLogger.log(message: "flutterCallbackInformation-callbackLibraryPath: \(String(describing: flutterCallbackInformation.callbackLibraryPath))") - - let flutterEngine: FlutterEngine = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) - flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) - SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) - - let backgroundMethodChannel: FlutterMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) - backgroundMethodChannel.setMethodCallHandler { (call, result) in - switch call.method { - case BackgroundMethods.initialized.rawValue: - result(true) - let locationData :[String: Any] = [ - "lat": location.coordinate.latitude, - "lon": location.coordinate.longitude, - "logging_enabled": SharedPrefsUtil.isLoggingEnabled(), - ] - backgroundMethodChannel.invokeMethod(BackgroundMethods.onLocationUpdate.rawValue, arguments: locationData, result: { flutterResult in + do { + guard let location = locations.last else { + CustomLogger.log(message: "No location ...") + return + } + + CustomLogger.log(message: "NEW LOCATION: \(location.coordinate.latitude): \(location.coordinate.longitude)") + + guard let callbackHandle = SharedPrefsUtil.getCallbackHandle(), + let flutterCallbackInformation = FlutterCallbackCache.lookupCallbackInformation(callbackHandle) + else { return } + + CustomLogger.log(message: "callbackHandle: \(callbackHandle)") + CustomLogger.log(message: "flutterCallbackInformation: \(flutterCallbackInformation)") + CustomLogger.log(message: "flutterCallbackInformation-name: \(String(describing: flutterCallbackInformation.callbackName))") + CustomLogger.log(message: "flutterCallbackInformation-callbackLibraryPath: \(String(describing: flutterCallbackInformation.callbackLibraryPath))") + + let flutterEngine: FlutterEngine = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) + flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) + SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) + + let backgroundMethodChannel: FlutterMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) + backgroundMethodChannel.setMethodCallHandler { (call, result) in + switch call.method { + case BackgroundMethods.initialized.rawValue: + result(true) + let locationData :[String: Any] = [ + "lat": location.coordinate.latitude, + "lon": location.coordinate.longitude, + "logging_enabled": SharedPrefsUtil.isLoggingEnabled(), + ] + backgroundMethodChannel.invokeMethod(BackgroundMethods.onLocationUpdate.rawValue, arguments: locationData, result: { flutterResult in + flutterEngine.destroyContext() + }) + default: + result(FlutterMethodNotImplemented) flutterEngine.destroyContext() - }) - default: - result(FlutterMethodNotImplemented) - flutterEngine.destroyContext() + } } + } catch let error { + CustomLogger.log(message: "CRASH!!! : \(error.localizedDescription)") } } } From d8862866f3d4cf1eaff68c2b29ebaa0cd9edb5c1 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 09:16:55 +0100 Subject: [PATCH 03/21] Refactorred the background ios implementation --- ...SwiftBackgroundLocationTrackerPlugin.swift | 92 ++++++++++--------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 5183944..abc4440 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -6,10 +6,14 @@ public class SwiftBackgroundLocationTrackerPlugin: FlutterPluginAppLifeCycleDele static let identifier = "com.icapps.background_location_tracker" - private let flutterThreadLabelPrefix = "\(identifier).BackgroundLocationTracker" + private static let flutterThreadLabelPrefix = "\(identifier).BackgroundLocationTracker" private static var foregroundChannel: ForegroundChannel? = nil + private static var flutterEngine: FlutterEngine? = nil + private static var backgroundMethodChannel: FlutterMethodChannel? = nil + + private static var flutterPluginRegistrantCallback: FlutterPluginRegistrantCallback? private let locationManager = LocationManager.shared() @@ -35,6 +39,25 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { locationManager.delegate = self SwiftBackgroundLocationTrackerPlugin.foregroundChannel?.handle(call, result: result) } + + public static func getFlutterEngine()-> FlutterEngine? { + if (flutterEngine == nil){ + guard let callbackHandle = SharedPrefsUtil.getCallbackHandle(), + let flutterCallbackInformation = FlutterCallbackCache.lookupCallbackInformation(callbackHandle) + else { return nil } + + flutterEngine = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) + flutterEngine!.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) + SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine!) + } + + return flutterEngine; + } + + public static func getBackgroundMethodChannel(flutterEngine: FlutterEngine)-> FlutterMethodChannel? { + backgroundMethodChannel = backgroundMethodChannel ?? FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) + return backgroundMethodChannel + } } fileprivate enum BackgroundMethods: String { @@ -46,47 +69,34 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { private static let BACKGROUND_CHANNEL_NAME = "com.icapps.background_location_tracker/background_channel" public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { - do { - guard let location = locations.last else { - CustomLogger.log(message: "No location ...") - return - } - - CustomLogger.log(message: "NEW LOCATION: \(location.coordinate.latitude): \(location.coordinate.longitude)") - - guard let callbackHandle = SharedPrefsUtil.getCallbackHandle(), - let flutterCallbackInformation = FlutterCallbackCache.lookupCallbackInformation(callbackHandle) - else { return } - - CustomLogger.log(message: "callbackHandle: \(callbackHandle)") - CustomLogger.log(message: "flutterCallbackInformation: \(flutterCallbackInformation)") - CustomLogger.log(message: "flutterCallbackInformation-name: \(String(describing: flutterCallbackInformation.callbackName))") - CustomLogger.log(message: "flutterCallbackInformation-callbackLibraryPath: \(String(describing: flutterCallbackInformation.callbackLibraryPath))") - - let flutterEngine: FlutterEngine = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) - flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) - SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) - - let backgroundMethodChannel: FlutterMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) - backgroundMethodChannel.setMethodCallHandler { (call, result) in - switch call.method { - case BackgroundMethods.initialized.rawValue: - result(true) - let locationData :[String: Any] = [ - "lat": location.coordinate.latitude, - "lon": location.coordinate.longitude, - "logging_enabled": SharedPrefsUtil.isLoggingEnabled(), - ] - backgroundMethodChannel.invokeMethod(BackgroundMethods.onLocationUpdate.rawValue, arguments: locationData, result: { flutterResult in - flutterEngine.destroyContext() - }) - default: - result(FlutterMethodNotImplemented) - flutterEngine.destroyContext() - } + guard let location = locations.last else { + CustomLogger.log(message: "No location ...") + return + } + guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else { + CustomLogger.log(message: "No flutter engine available ...") + return + } + + CustomLogger.log(message: "NEW LOCATION: \(location.coordinate.latitude): \(location.coordinate.longitude)") + + guard let backgroundMethodChannel = SwiftBackgroundLocationTrackerPlugin.getBackgroundMethodChannel(flutterEngine: flutterEngine) else { + CustomLogger.log(message: "No flutter background channel method available ...") + return + } + backgroundMethodChannel.setMethodCallHandler { (call, result) in + switch call.method { + case BackgroundMethods.initialized.rawValue: + result(true) + let locationData :[String: Any] = [ + "lat": location.coordinate.latitude, + "lon": location.coordinate.longitude, + "logging_enabled": SharedPrefsUtil.isLoggingEnabled(), + ] + backgroundMethodChannel.invokeMethod(BackgroundMethods.onLocationUpdate.rawValue, arguments: locationData, result: { flutterResult in }) + default: + result(FlutterMethodNotImplemented) } - } catch let error { - CustomLogger.log(message: "CRASH!!! : \(error.localizedDescription)") } } } From 4160015b82bf76cc44622c72c16769952b8fd155 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 09:33:48 +0100 Subject: [PATCH 04/21] Added extra logs --- ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index abc4440..0f0cfce 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -84,6 +84,7 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { CustomLogger.log(message: "No flutter background channel method available ...") return } + CustomLogger.log(message: "Ready to set background channel") backgroundMethodChannel.setMethodCallHandler { (call, result) in switch call.method { case BackgroundMethods.initialized.rawValue: @@ -93,8 +94,13 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { "lon": location.coordinate.longitude, "logging_enabled": SharedPrefsUtil.isLoggingEnabled(), ] - backgroundMethodChannel.invokeMethod(BackgroundMethods.onLocationUpdate.rawValue, arguments: locationData, result: { flutterResult in }) + + CustomLogger.log(message: "INITIALIZED, ready to send location updates") + backgroundMethodChannel.invokeMethod(BackgroundMethods.onLocationUpdate.rawValue, arguments: locationData, result: { flutterResult in + CustomLogger.log(message: "Received result: \(flutterResult.debugDescription)") + }) default: + CustomLogger.log(message: "Not implemented method -> \(call.method)") result(FlutterMethodNotImplemented) } } From d8b1525f886bb0d41b6b05dedb291350162220e0 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 09:50:00 +0100 Subject: [PATCH 05/21] Removed the static backgroundMethodChannel --- .../SwiftBackgroundLocationTrackerPlugin.swift | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 0f0cfce..2570510 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -10,9 +10,7 @@ public class SwiftBackgroundLocationTrackerPlugin: FlutterPluginAppLifeCycleDele private static var foregroundChannel: ForegroundChannel? = nil - private static var flutterEngine: FlutterEngine? = nil - private static var backgroundMethodChannel: FlutterMethodChannel? = nil - + private static var flutterEngine: FlutterEngine? = nil private static var flutterPluginRegistrantCallback: FlutterPluginRegistrantCallback? @@ -53,11 +51,6 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { return flutterEngine; } - - public static func getBackgroundMethodChannel(flutterEngine: FlutterEngine)-> FlutterMethodChannel? { - backgroundMethodChannel = backgroundMethodChannel ?? FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) - return backgroundMethodChannel - } } fileprivate enum BackgroundMethods: String { @@ -80,11 +73,7 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { CustomLogger.log(message: "NEW LOCATION: \(location.coordinate.latitude): \(location.coordinate.longitude)") - guard let backgroundMethodChannel = SwiftBackgroundLocationTrackerPlugin.getBackgroundMethodChannel(flutterEngine: flutterEngine) else { - CustomLogger.log(message: "No flutter background channel method available ...") - return - } - CustomLogger.log(message: "Ready to set background channel") + let backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) backgroundMethodChannel.setMethodCallHandler { (call, result) in switch call.method { case BackgroundMethods.initialized.rawValue: From 257a0d3ecfda45fdf5227ce5c3f5898bd6458db5 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 10:15:51 +0100 Subject: [PATCH 06/21] Rerun the flutterEngine.run method --- ...SwiftBackgroundLocationTrackerPlugin.swift | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 2570510..556a612 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -39,16 +39,7 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { } public static func getFlutterEngine()-> FlutterEngine? { - if (flutterEngine == nil){ - guard let callbackHandle = SharedPrefsUtil.getCallbackHandle(), - let flutterCallbackInformation = FlutterCallbackCache.lookupCallbackInformation(callbackHandle) - else { return nil } - - flutterEngine = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) - flutterEngine!.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) - SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine!) - } - + flutterEngine = flutterEngine ?? FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) return flutterEngine; } } @@ -66,13 +57,23 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { CustomLogger.log(message: "No location ...") return } + guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else { CustomLogger.log(message: "No flutter engine available ...") return } + guard let callbackHandle = SharedPrefsUtil.getCallbackHandle(), + let flutterCallbackInformation = FlutterCallbackCache.lookupCallbackInformation(callbackHandle) else { + CustomLogger.log(message: "No flutter callback cache ...") + return + } + CustomLogger.log(message: "NEW LOCATION: \(location.coordinate.latitude): \(location.coordinate.longitude)") + flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) + SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) + let backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) backgroundMethodChannel.setMethodCallHandler { (call, result) in switch call.method { From ba9c30ddbf2cf36614e419dc143b0c815915d3c4 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 10:23:43 +0100 Subject: [PATCH 07/21] Only executre the register plugins once --- ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 556a612..4565b9b 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -40,6 +40,7 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { public static func getFlutterEngine()-> FlutterEngine? { flutterEngine = flutterEngine ?? FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) + SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) return flutterEngine; } } @@ -72,7 +73,6 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { CustomLogger.log(message: "NEW LOCATION: \(location.coordinate.latitude): \(location.coordinate.longitude)") flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) - SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) let backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) backgroundMethodChannel.setMethodCallHandler { (call, result) in From 97e40ea75534372d9bb69739abc545c7245bce21 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 10:25:01 +0100 Subject: [PATCH 08/21] Flutter engine force. unwrap --- ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 4565b9b..d8ad157 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -40,7 +40,7 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { public static func getFlutterEngine()-> FlutterEngine? { flutterEngine = flutterEngine ?? FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) - SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) + SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine!) return flutterEngine; } } From 4b721491afccf51d7ccb84f08ce1710e62eb502c Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 10:38:59 +0100 Subject: [PATCH 09/21] Only register the iOS plugins once --- ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index d8ad157..1b03d5e 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -39,8 +39,10 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { } public static func getFlutterEngine()-> FlutterEngine? { - flutterEngine = flutterEngine ?? FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) - SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine!) + if (flutterEngine == nil) { + flutterEngine = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) + SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine!) + } return flutterEngine; } } From 3c9cd81ce0fd5edf709d3e827c8d89eea8a72716 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 10:54:26 +0100 Subject: [PATCH 10/21] Has registered plugins --- ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 1b03d5e..f46b3a1 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -10,7 +10,8 @@ public class SwiftBackgroundLocationTrackerPlugin: FlutterPluginAppLifeCycleDele private static var foregroundChannel: ForegroundChannel? = nil - private static var flutterEngine: FlutterEngine? = nil + private static var flutterEngine: FlutterEngine? = nil + private static var hasRegisteredPlugins = false private static var flutterPluginRegistrantCallback: FlutterPluginRegistrantCallback? @@ -41,7 +42,7 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { public static func getFlutterEngine()-> FlutterEngine? { if (flutterEngine == nil) { flutterEngine = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) - SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine!) + hasRegisteredPlugins = false } return flutterEngine; } @@ -76,6 +77,11 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) + if (!SwiftBackgroundLocationTrackerPlugin.hasRegisteredPlugins) { + SwiftBackgroundLocationTrackerPlugin.hasRegisteredPlugins = true + SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) + } + let backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) backgroundMethodChannel.setMethodCallHandler { (call, result) in switch call.method { From fa8b2a70d409c23cb2b37b4df196cb7370f9b4f2 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 11:30:32 +0100 Subject: [PATCH 11/21] Background channel rewrite --- ...SwiftBackgroundLocationTrackerPlugin.swift | 101 +++++++++++------- 1 file changed, 62 insertions(+), 39 deletions(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index f46b3a1..38451b3 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -9,9 +9,12 @@ public class SwiftBackgroundLocationTrackerPlugin: FlutterPluginAppLifeCycleDele private static let flutterThreadLabelPrefix = "\(identifier).BackgroundLocationTracker" private static var foregroundChannel: ForegroundChannel? = nil + private static var backgroundMethodChannel: FlutterMethodChannel? = nil private static var flutterEngine: FlutterEngine? = nil private static var hasRegisteredPlugins = false + private static var initializedBackgroundCallbacks = false + private static var locationData: [String: Any]? = nil private static var flutterPluginRegistrantCallback: FlutterPluginRegistrantCallback? @@ -41,10 +44,55 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { public static func getFlutterEngine()-> FlutterEngine? { if (flutterEngine == nil) { - flutterEngine = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) - hasRegisteredPlugins = false + let flutterEngine = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) + + guard let callbackHandle = SharedPrefsUtil.getCallbackHandle(), + let flutterCallbackInformation = FlutterCallbackCache.lookupCallbackInformation(callbackHandle) else { + CustomLogger.log(message: "No flutter callback cache ...") + return nil + } + flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) + SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) + self.flutterEngine = flutterEngine } - return flutterEngine; + return flutterEngine + } + + public static func getBackgroundMethodChannel(flutterEngine: FlutterEngine)-> FlutterMethodChannel? { + if (backgroundMethodChannel == nil) { + let backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) + backgroundMethodChannel.setMethodCallHandler { (call, result) in + switch call.method { + case BackgroundMethods.initialized.rawValue: + initializedBackgroundCallbacks = true + let locationData = SwiftBackgroundLocationTrackerPlugin.locationData + if let data = locationData { + sendLocationupdate(locationData: data) + } + result(true) + default: + CustomLogger.log(message: "Not implemented method -> \(call.method)") + result(FlutterMethodNotImplemented) + } + } + self.backgroundMethodChannel = backgroundMethodChannel + } + return backgroundMethodChannel + } + + public static func sendLocationupdate(locationData: [String: Any]){ + guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else { + CustomLogger.log(message: "No flutter engine available ...") + return + } + + guard let backgroundMethodChannel = SwiftBackgroundLocationTrackerPlugin.getBackgroundMethodChannel(flutterEngine: flutterEngine) else { + CustomLogger.log(message: "No background channel available ...") + return + } + backgroundMethodChannel.invokeMethod(BackgroundMethods.onLocationUpdate.rawValue, arguments: locationData, result: { flutterResult in + CustomLogger.log(message: "Received result: \(flutterResult.debugDescription)") + }) } } @@ -62,45 +110,20 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { return } - guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else { - CustomLogger.log(message: "No flutter engine available ...") - return - } - - guard let callbackHandle = SharedPrefsUtil.getCallbackHandle(), - let flutterCallbackInformation = FlutterCallbackCache.lookupCallbackInformation(callbackHandle) else { - CustomLogger.log(message: "No flutter callback cache ...") - return - } - CustomLogger.log(message: "NEW LOCATION: \(location.coordinate.latitude): \(location.coordinate.longitude)") - flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) - - if (!SwiftBackgroundLocationTrackerPlugin.hasRegisteredPlugins) { - SwiftBackgroundLocationTrackerPlugin.hasRegisteredPlugins = true - SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) - } + let locationData: [String: Any] = [ + "lat": location.coordinate.latitude, + "lon": location.coordinate.longitude, + "logging_enabled": SharedPrefsUtil.isLoggingEnabled(), + ] - let backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) - backgroundMethodChannel.setMethodCallHandler { (call, result) in - switch call.method { - case BackgroundMethods.initialized.rawValue: - result(true) - let locationData :[String: Any] = [ - "lat": location.coordinate.latitude, - "lon": location.coordinate.longitude, - "logging_enabled": SharedPrefsUtil.isLoggingEnabled(), - ] - - CustomLogger.log(message: "INITIALIZED, ready to send location updates") - backgroundMethodChannel.invokeMethod(BackgroundMethods.onLocationUpdate.rawValue, arguments: locationData, result: { flutterResult in - CustomLogger.log(message: "Received result: \(flutterResult.debugDescription)") - }) - default: - CustomLogger.log(message: "Not implemented method -> \(call.method)") - result(FlutterMethodNotImplemented) - } + if (SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacks) { + CustomLogger.log(message: "INITIALIZED, ready to send location updates") + SwiftBackgroundLocationTrackerPlugin.sendLocationupdate(locationData: locationData) + } else { + CustomLogger.log(message: "NOT YET INITIALIZED. Cache the location data") + SwiftBackgroundLocationTrackerPlugin.locationData = locationData } } } From e1927d8e80f392c638488cdeaef22d97e637e139 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 11:46:45 +0100 Subject: [PATCH 12/21] Make sure the init background channel is triggered --- ...SwiftBackgroundLocationTrackerPlugin.swift | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 38451b3..0ece81c 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -14,6 +14,7 @@ public class SwiftBackgroundLocationTrackerPlugin: FlutterPluginAppLifeCycleDele private static var flutterEngine: FlutterEngine? = nil private static var hasRegisteredPlugins = false private static var initializedBackgroundCallbacks = false + private static var initializedBackgroundCallbacksStarted = false private static var locationData: [String: Any]? = nil private static var flutterPluginRegistrantCallback: FlutterPluginRegistrantCallback? @@ -58,12 +59,13 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { return flutterEngine } - public static func getBackgroundMethodChannel(flutterEngine: FlutterEngine)-> FlutterMethodChannel? { + public static func initBackgroundMethodChannel(flutterEngine: FlutterEngine) { if (backgroundMethodChannel == nil) { let backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) backgroundMethodChannel.setMethodCallHandler { (call, result) in switch call.method { case BackgroundMethods.initialized.rawValue: + CustomLogger.log(message: "Initialized ...") initializedBackgroundCallbacks = true let locationData = SwiftBackgroundLocationTrackerPlugin.locationData if let data = locationData { @@ -77,16 +79,10 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { } self.backgroundMethodChannel = backgroundMethodChannel } - return backgroundMethodChannel } public static func sendLocationupdate(locationData: [String: Any]){ - guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else { - CustomLogger.log(message: "No flutter engine available ...") - return - } - - guard let backgroundMethodChannel = SwiftBackgroundLocationTrackerPlugin.getBackgroundMethodChannel(flutterEngine: flutterEngine) else { + guard let backgroundMethodChannel = SwiftBackgroundLocationTrackerPlugin.backgroundMethodChannel else { CustomLogger.log(message: "No background channel available ...") return } @@ -124,6 +120,16 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { } else { CustomLogger.log(message: "NOT YET INITIALIZED. Cache the location data") SwiftBackgroundLocationTrackerPlugin.locationData = locationData + + if (!SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacksStarted){ + SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacksStarted = true + + guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else { + CustomLogger.log(message: "No Flutter engine available ...") + return + } + SwiftBackgroundLocationTrackerPlugin.initBackgroundMethodChannel(flutterEngine: flutterEngine) + } } } } From be7d2933836d7258447f32333a608b25fe7ce6dd Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 12:11:17 +0100 Subject: [PATCH 13/21] pause location updates automaticly should be set to false --- ios/Classes/LocationManager.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/ios/Classes/LocationManager.swift b/ios/Classes/LocationManager.swift index 767375f..273f826 100644 --- a/ios/Classes/LocationManager.swift +++ b/ios/Classes/LocationManager.swift @@ -14,6 +14,7 @@ class LocationManager { let manager = CLLocationManager() manager.activityType = .automotiveNavigation manager.desiredAccuracy = kCLLocationAccuracyBest + manager.pausesLocationUpdatesAutomatically = false if #available(iOS 11, *) { manager.showsBackgroundLocationIndicator = true } From 45efd9e041b3f2c99236f647d48171f6ee1e04eb Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 14:27:39 +0100 Subject: [PATCH 14/21] Not initialized bug. --- ...SwiftBackgroundLocationTrackerPlugin.swift | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 0ece81c..617b26e 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -14,7 +14,6 @@ public class SwiftBackgroundLocationTrackerPlugin: FlutterPluginAppLifeCycleDele private static var flutterEngine: FlutterEngine? = nil private static var hasRegisteredPlugins = false private static var initializedBackgroundCallbacks = false - private static var initializedBackgroundCallbacksStarted = false private static var locationData: [String: Any]? = nil private static var flutterPluginRegistrantCallback: FlutterPluginRegistrantCallback? @@ -61,15 +60,18 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { public static func initBackgroundMethodChannel(flutterEngine: FlutterEngine) { if (backgroundMethodChannel == nil) { - let backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) - backgroundMethodChannel.setMethodCallHandler { (call, result) in + CustomLogger.log(message: "backgroundMethodChannel is nil, we will create a new background method channel.") + backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) + backgroundMethodChannel!.setMethodCallHandler { (call, result) in switch call.method { case BackgroundMethods.initialized.rawValue: - CustomLogger.log(message: "Initialized ...") initializedBackgroundCallbacks = true let locationData = SwiftBackgroundLocationTrackerPlugin.locationData if let data = locationData { + CustomLogger.log(message: "Initialized with a cached location, we will send it!") sendLocationupdate(locationData: data) + } else { + CustomLogger.log(message: "Initialized without a cached location") } result(true) default: @@ -77,7 +79,6 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { result(FlutterMethodNotImplemented) } } - self.backgroundMethodChannel = backgroundMethodChannel } } @@ -121,15 +122,11 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { CustomLogger.log(message: "NOT YET INITIALIZED. Cache the location data") SwiftBackgroundLocationTrackerPlugin.locationData = locationData - if (!SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacksStarted){ - SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacksStarted = true - - guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else { - CustomLogger.log(message: "No Flutter engine available ...") - return - } - SwiftBackgroundLocationTrackerPlugin.initBackgroundMethodChannel(flutterEngine: flutterEngine) + guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else { + CustomLogger.log(message: "No Flutter engine available ...") + return } + SwiftBackgroundLocationTrackerPlugin.initBackgroundMethodChannel(flutterEngine: flutterEngine) } } } From 3cc0316b3300edb04fbf57e6e2e08e4934eaec1f Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 15:20:04 +0100 Subject: [PATCH 15/21] Try to send to location update anyway --- ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 617b26e..b12c423 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -127,6 +127,9 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { return } SwiftBackgroundLocationTrackerPlugin.initBackgroundMethodChannel(flutterEngine: flutterEngine) + + CustomLogger.log(message: "Let's try to send the location update anyway ...") + SwiftBackgroundLocationTrackerPlugin.sendLocationupdate(locationData: locationData) } } } From c5cfcb66aa0acbf1fa7457862f2f4b3c20fb3e32 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Thu, 18 Feb 2021 16:01:24 +0100 Subject: [PATCH 16/21] Revert the initializedBackgroundCallbackStarted --- ...SwiftBackgroundLocationTrackerPlugin.swift | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index b12c423..726dfb2 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -14,6 +14,7 @@ public class SwiftBackgroundLocationTrackerPlugin: FlutterPluginAppLifeCycleDele private static var flutterEngine: FlutterEngine? = nil private static var hasRegisteredPlugins = false private static var initializedBackgroundCallbacks = false + private static var initializedBackgroundCallbacksStarted = false private static var locationData: [String: Any]? = nil private static var flutterPluginRegistrantCallback: FlutterPluginRegistrantCallback? @@ -60,18 +61,16 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { public static func initBackgroundMethodChannel(flutterEngine: FlutterEngine) { if (backgroundMethodChannel == nil) { - CustomLogger.log(message: "backgroundMethodChannel is nil, we will create a new background method channel.") - backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) - backgroundMethodChannel!.setMethodCallHandler { (call, result) in + let backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) + backgroundMethodChannel.setMethodCallHandler { (call, result) in switch call.method { case BackgroundMethods.initialized.rawValue: initializedBackgroundCallbacks = true - let locationData = SwiftBackgroundLocationTrackerPlugin.locationData - if let data = locationData { - CustomLogger.log(message: "Initialized with a cached location, we will send it!") + if let data = SwiftBackgroundLocationTrackerPlugin.locationData { + CustomLogger.log(message: "Initialized with cached value, sending location update") sendLocationupdate(locationData: data) } else { - CustomLogger.log(message: "Initialized without a cached location") + CustomLogger.log(message: "Initialized without cached value") } result(true) default: @@ -79,6 +78,7 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { result(FlutterMethodNotImplemented) } } + self.backgroundMethodChannel = backgroundMethodChannel } } @@ -122,14 +122,15 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { CustomLogger.log(message: "NOT YET INITIALIZED. Cache the location data") SwiftBackgroundLocationTrackerPlugin.locationData = locationData - guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else { - CustomLogger.log(message: "No Flutter engine available ...") - return - } - SwiftBackgroundLocationTrackerPlugin.initBackgroundMethodChannel(flutterEngine: flutterEngine) + if (!SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacksStarted){ + SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacksStarted = true - CustomLogger.log(message: "Let's try to send the location update anyway ...") - SwiftBackgroundLocationTrackerPlugin.sendLocationupdate(locationData: locationData) + guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else { + CustomLogger.log(message: "No Flutter engine available ...") + return + } + SwiftBackgroundLocationTrackerPlugin.initBackgroundMethodChannel(flutterEngine: flutterEngine) + } } } } From ff6cb5acb8d868930e0073e39eae0f470d19d811 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Fri, 19 Feb 2021 09:04:53 +0100 Subject: [PATCH 17/21] Updated the logger & cleanup the flutter engine if the run method could not be called correctly --- ios/Classes/CustomLogger.swift | 8 ++++---- .../SwiftBackgroundLocationTrackerPlugin.swift | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/ios/Classes/CustomLogger.swift b/ios/Classes/CustomLogger.swift index 61c07dd..6671844 100644 --- a/ios/Classes/CustomLogger.swift +++ b/ios/Classes/CustomLogger.swift @@ -10,11 +10,11 @@ import Foundation struct CustomLogger { static func log(message: String) { - if #available(iOS 10.0, *) { - let app = OSLog(subsystem: "com.icapps.background_location_tracker", category: "background tracker") - os_log("🔥 background-location log: %{public}@", log: app, type: .error, message) - } if SharedPrefsUtil.isLoggingEnabled() { + if #available(iOS 10.0, *) { + let app = OSLog(subsystem: "com.icapps.background_location_tracker", category: "background tracker") + os_log("🔥 background-location log: %{public}@", log: app, type: .error, message) + } print(message) } } diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 726dfb2..159e505 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -52,9 +52,14 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { CustomLogger.log(message: "No flutter callback cache ...") return nil } - flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) - SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) - self.flutterEngine = flutterEngine + let success = flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) + if (success) { + SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) + self.flutterEngine = flutterEngine + } else { + flutterEngine.destroyContext() + CustomLogger.log(message: "FlutterEngine.run returned `false` this means that the callback function could not be fount, triggered or there was a crash in your callback function.") + } } return flutterEngine } From f9bfc24bbb762120858098848ced3ca32746ad94 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Fri, 19 Feb 2021 09:12:59 +0100 Subject: [PATCH 18/21] Added an extra success log --- ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 159e505..015b209 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -53,6 +53,8 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { return nil } let success = flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) + + CustomLogger.log(message: "FlutterEngine.run returned `\(success)`") if (success) { SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) self.flutterEngine = flutterEngine From 005c2818aa4cddddce9fda97c1ae4b6a02651f47 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Fri, 19 Feb 2021 09:20:29 +0100 Subject: [PATCH 19/21] Move the log --- ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 015b209..9eaeae0 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -59,8 +59,8 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) self.flutterEngine = flutterEngine } else { + CustomLogger.log(message: "FlutterEngine.run returned `false` we will cleanup the flutterEngine") flutterEngine.destroyContext() - CustomLogger.log(message: "FlutterEngine.run returned `false` this means that the callback function could not be fount, triggered or there was a crash in your callback function.") } } return flutterEngine From e213c4f9ba17244c81e9e8a2377ce3a3d11f429c Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Fri, 19 Feb 2021 10:33:45 +0100 Subject: [PATCH 20/21] Removed options bracets --- ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index 9eaeae0..e39296f 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -55,7 +55,7 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { let success = flutterEngine.run(withEntrypoint: flutterCallbackInformation.callbackName, libraryURI: flutterCallbackInformation.callbackLibraryPath) CustomLogger.log(message: "FlutterEngine.run returned `\(success)`") - if (success) { + if success { SwiftBackgroundLocationTrackerPlugin.flutterPluginRegistrantCallback?(flutterEngine) self.flutterEngine = flutterEngine } else { From e4d8c9f61f031d39679c6f8dd510e8d65fff12ea Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Fri, 19 Feb 2021 10:40:43 +0100 Subject: [PATCH 21/21] Fixed if statements --- ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift index e39296f..033ae0a 100644 --- a/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift +++ b/ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift @@ -44,7 +44,7 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { } public static func getFlutterEngine()-> FlutterEngine? { - if (flutterEngine == nil) { + if flutterEngine == nil { let flutterEngine = FlutterEngine(name: flutterThreadLabelPrefix, project: nil, allowHeadlessExecution: true) guard let callbackHandle = SharedPrefsUtil.getCallbackHandle(), @@ -67,7 +67,7 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin { } public static func initBackgroundMethodChannel(flutterEngine: FlutterEngine) { - if (backgroundMethodChannel == nil) { + if backgroundMethodChannel == nil { let backgroundMethodChannel = FlutterMethodChannel(name: SwiftBackgroundLocationTrackerPlugin.BACKGROUND_CHANNEL_NAME, binaryMessenger: flutterEngine.binaryMessenger) backgroundMethodChannel.setMethodCallHandler { (call, result) in switch call.method { @@ -122,14 +122,14 @@ extension SwiftBackgroundLocationTrackerPlugin: CLLocationManagerDelegate { "logging_enabled": SharedPrefsUtil.isLoggingEnabled(), ] - if (SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacks) { + if SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacks { CustomLogger.log(message: "INITIALIZED, ready to send location updates") SwiftBackgroundLocationTrackerPlugin.sendLocationupdate(locationData: locationData) } else { CustomLogger.log(message: "NOT YET INITIALIZED. Cache the location data") SwiftBackgroundLocationTrackerPlugin.locationData = locationData - if (!SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacksStarted){ + if !SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacksStarted { SwiftBackgroundLocationTrackerPlugin.initializedBackgroundCallbacksStarted = true guard let flutterEngine = SwiftBackgroundLocationTrackerPlugin.getFlutterEngine() else {