如何在iOS中使用单独的FlutterEngine注册Flutter应用程序插件

问题描述

由于我的iOS委托方法didUpdateLocation被调用,因此我的Flutter应用程序的隔离功能后台醒来。

当didUpdateLocation被调用时,我能够在颤动侧看到打印日志,但是当我尝试使用path_provider时,却遇到了缺少的插件异常。

我相信我的问题是我没有向在didUpdateLocations中产生的Flutter Engine注册应用程序插件

注意:本机iOS端上的此位置逻辑当前在Main Flutter应用程序中,而不是插件

主要问题:

  • 在哪里可以在iOS本机中为飞镖隔离/无脑扑动引擎注册插件

奖金问题:

  • 我需要手动销毁颤振引擎吗? (请参阅cleanUpFlutterResources)
  • 关于在应用程序终止时运行的最佳方法,是最好的做法是创建一个Flutter引擎,并在每次调用后台iOS委托回调时销毁它,还是我应该使用一个运行所有后台程序的后台通道创建一个隔离时间?
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {        
        if let location = locations.first {
            
            guard let callbackHandle = LocalStorage.getStoredCallbackHandle(),let FlutterCallbackinformation = FlutterCallbackCache.lookupCallbackinformation(callbackHandle) else {
                return
            }
            
            var FlutterEngine: FlutterEngine? = FlutterEngine(name: "atc_location_swift.BGThread",project: nil,allowHeadlessExecution: true)
            
            FlutterEngine!.run(withEntrypoint: FlutterCallbackinformation.callbackName,libraryURI: FlutterCallbackinformation.callbackLibraryPath)
            
            FlutterEngine!.registrar(forPlugin: "FLTPathProviderPlugin")
            
            var backgroundMethodChannel: FlutterMethodChannel? = FlutterMethodChannel(name: "plugins.atc.io/location_background",binaryMessenger: FlutterEngine!.binaryMessenger)
            
            func cleanupFlutterResources() {
                FlutterEngine?.destroyContext()
                backgroundMethodChannel = nil
                FlutterEngine = nil
            }
            
            backgroundMethodChannel?.setMethodCallHandler { (call,result) in
                    result(true)
                    backgroundMethodChannel?.invokeMethod("triggerBackgroundTask",arguments: ["lat": 0.1,"lon": 0.2],result: { FlutterResult in
                        
                        print("Flutter result \(FlutterResult ?? "no result sent")")
                        
                        cleanupFlutterResources()
                    })
            }
            
            print("location: \(location.coordinate)")
        }
    }

这是我的飞镖隔离物。基本上我正在打印“现在调用回调-> $ Now”,但是当我尝试addItemToDataList()时,路径提供程序插件被引用并抛出缺少的插件异常

void callback() async {
  WidgetsFlutterBinding.ensureInitialized();

  const MethodChannel _backgroundChannel =
      MethodChannel('plugins.atc.io/location_background');

  _backgroundChannel.setMethodCallHandler((MethodCall call) async {
    try {
      WidgetsFlutterBinding.ensureInitialized();

      final String Now = DateTime.Now().toString();
      print("callback called Now -> $Now");
      await LocationDataSource.addItemToDataList(Now);
      return Future.value(true);
    } catch (e) {
      print("exception calling callback: $e");
      return Future.value(false);
    }
  });
  _backgroundChannel.invokeMethod('backgroundChannelInitialized');
}

这是我的GeneratedpluginRegistrant,似乎在调用Flutter run时,依赖项(path_provider)已在此处用本机代码注册,但是从飞镖隔离调用路径提供程序插件时,似乎并没有达到目的

#if __has_include(<path_provider/FLTPathProviderPlugin.h>)
#import <path_provider/FLTPathProviderPlugin.h>
#else
@import path_provider;
#endif

@implementation GeneratedpluginRegistrant

+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
  [FLTPathProviderPlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTPathProviderPlugin"]];
}

@end

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...