I'm using this background_locator plugin and a geofencing plugin in my Flutter app. In the location callback function of the background_locator plugin I create geofences using the geofencing plugin. This works fine on Android. On iOS the background_locator plugin functions as it should, but calling geofencing methods in the callback has no effect. Calling the geofencing methods outside of the callback works without problems.
I found some resources addressing this: https://github.com/flutter/flutter/issues/21925 and https://github.com/flutter/engine/pull/7843 and changed the code in my AppDelegate.swift accordingly, but this issue still occurs. Any advice is appreciated!
Below is my AppDelegate.swift
import UIKit
import Flutter
import GoogleMaps
func registerPlugins(registry: FlutterPluginRegistry) -> () {
if (!registry.hasPlugin("BackgroundLocatorPlugin")) {
BackgroundLocatorPlugin.register(with: registry.registrar(forPlugin: "BackgroundLocatorPlugin"))
}
if (!registry.hasPlugin("GeofencingPlugin")) {
GeofencingPlugin.register(with: registry.registrar(forPlugin: "GeofencingPlugin"))
}
if (!registry.hasPlugin("FlutterLocalNotificationsPlugin")) {
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
}
if (!registry.hasPlugin("FLTSharedPreferencesPlugin")) {
FLTSharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "FLTSharedPreferencesPlugin"))
}
if (!registry.hasPlugin("FlutterSecureStoragePlugin")) {
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
}
}
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GMSServices.provideAPIKey("$GOOGLE_MAPS_API_KEY")
GeneratedPluginRegistrant.register(with: self)
GeofencingPlugin.setPluginRegistrantCallback(registerPlugins)
BackgroundLocatorPlugin.setPluginRegistrantCallback(registerPlugins)
// iOS notification setup
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
// Prevent "old" notifications showing up after reinstallation
if(!UserDefaults.standard.bool(forKey: "Notification")) {
UIApplication.shared.cancelAllLocalNotifications()
UserDefaults.standard.set(true, forKey: "Notification")
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}