How can plugins be used in another plugin's callback in the background?

Viewed 995

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)
  }
}
1 Answers

I have face the issue with "FlutterGeofencing" that app will caught an exception "failed to set registerPlugins" and resolve the issue by set the callback

override func application(
       _ application: UIApplication,
       didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
   ) -> Bool {
       GeneratedPluginRegistrant.register(with: self)
       GeofencingPlugin.setPluginRegistrantCallback({_ in
       })
       return super.application(application, didFinishLaunchingWithOptions: launchOptions)
   }
Related