Skip to content

Commit 0e87230

Browse files
Add support for restoring after app kill in iOS
1 parent 6d829a3 commit 0e87230

File tree

7 files changed

+26
-1
lines changed

7 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
## 1.1.0 - 14-10-2021
1+
## 1.1.0 - xx-xx-2021
22
#Added
33
- Added more options to android config to specify update interval
44
- Added iOS specific options to control activity type and/or distance filter
5+
- Added iOS specific options to restart the tracking after killing the app
56
- Added option to Android specific options to only get updates every x meters
67

78
## 1.0.2 - 22-06-2021

example/lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Future<void> main() async {
1616
await BackgroundLocationTrackerManager.initialize(
1717
_backgroundCallback,
1818
config: const BackgroundLocationTrackerConfig(
19+
loggingEnabled: true,
1920
androidConfig: AndroidConfig(
2021
notificationIcon: 'explore',
2122
trackingInterval: Duration(seconds: 4),
@@ -24,6 +25,7 @@ Future<void> main() async {
2425
iOSConfig: IOSConfig(
2526
activityType: ActivityType.FITNESS,
2627
distanceFilterMeters: 5,
28+
restartAfterKill: true,
2729
),
2830
),
2931
);

ios/Classes/ForegroundChannel.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class ForegroundChannel : NSObject {
5151
let loggingEnabledKey = "logging_enabled"
5252
let activityTypeKey = "ios_activity_type"
5353
let distanceFilterKey = "ios_distance_filter"
54+
let restartAfterKillKey = "ios_restart_after_kill"
5455
let map = call.arguments as? [String: Any]
5556
guard let callbackDispatcherHandle = map?[callBackHandleKey] else {
5657
result(false)
@@ -60,6 +61,7 @@ public class ForegroundChannel : NSObject {
6061

6162
let loggingEnabled: Bool = map?[loggingEnabledKey] as? Bool ?? false
6263
SharedPrefsUtil.saveLoggingEnabled(loggingEnabled)
64+
SharedPrefsUtil.saveRestartAfterKillEnabled(map?[restartAfterKillKey] as? Bool ?? false)
6365

6466
let activityType: CLActivityType
6567
switch (map?[activityTypeKey] as? String ?? "AUTOMOTIVE") {

ios/Classes/SharedPrefsUtil.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct SharedPrefsUtil {
1212

1313
private static let KEY_CALLBACK_HANDLER = "background.location.tracker.manager.CALLBACK_DISPATCHER_HANDLE_KEY"
1414
private static let KEY_IS_TRACKING = "background.location.tracker.manager.IS_TRACKING"
15+
private static let KEY_IS_RESTART_AFTER_KILL = "background.location.tracker.manager.RESTART_AFTER_KILL"
1516
private static let KEY_LOGGING_ENABLED = "background.location.tracker.manager.LOGGIN_ENABLED"
1617
private static let KEY_DISTANCE_FILTER = "background.location.tracker.manager.DISTANCE_FILTER"
1718
private static let KEY_ACTIVITY_TYPE = "background.location.tracker.manager.KEY_ACTIVITY_TYPE"
@@ -38,6 +39,14 @@ struct SharedPrefsUtil {
3839
return getValue(for: SharedPrefsUtil.KEY_IS_TRACKING) ?? false
3940
}
4041

42+
static func restartAfterKill() -> Bool {
43+
return getValue(for: SharedPrefsUtil.KEY_IS_RESTART_AFTER_KILL) ?? false
44+
}
45+
46+
static func saveRestartAfterKillEnabled(_ isRestartEnabled: Bool) {
47+
store(isRestartEnabled, key: SharedPrefsUtil.KEY_IS_RESTART_AFTER_KILL)
48+
}
49+
4150
static func saveLoggingEnabled(_ isLoggingEnabled: Bool) {
4251
store(isLoggingEnabled, key: SharedPrefsUtil.KEY_LOGGING_ENABLED)
4352
}

ios/Classes/SwiftBackgroundLocationTrackerPlugin.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ extension SwiftBackgroundLocationTrackerPlugin: FlutterPlugin {
3636
let instance = SwiftBackgroundLocationTrackerPlugin()
3737
registrar.addMethodCallDelegate(instance, channel: methodChannel)
3838
registrar.addApplicationDelegate(instance)
39+
40+
if (SharedPrefsUtil.isTracking() && SharedPrefsUtil.restartAfterKill()) {
41+
instance.locationManager.delegate = instance
42+
instance.locationManager.startUpdatingLocation()
43+
}
3944
}
4045

4146
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {

lib/src/channel/foreground_channel.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class ForegroundChannel {
4141
'android_distance_filter': config.androidConfig.distanceFilterMeters,
4242
'ios_activity_type': _activityTypeString(config.iOSConfig.activityType),
4343
'ios_distance_filter': config.iOSConfig.distanceFilterMeters,
44+
'ios_restart_after_kill': config.iOSConfig.restartAfterKill,
4445
},
4546
);
4647
}

lib/src/model/config/ios_config.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ class IOSConfig {
1010
/// The type of activity, default to [ActivityType.AUTOMOTIVE]
1111
final ActivityType activityType;
1212

13+
/// Restart the location tracker when the app is recreated
14+
/// after it has been killed by the user. Defaults to false
15+
final bool restartAfterKill;
16+
1317
/// The distance in meters that should be moved before updates are sent.
1418
/// Defaults to no filter (null)
1519
final int? distanceFilterMeters;
1620

1721
const IOSConfig({
1822
this.activityType = ActivityType.AUTOMOTIVE,
1923
this.distanceFilterMeters,
24+
this.restartAfterKill = false,
2025
});
2126
}
2227

0 commit comments

Comments
 (0)