iOS项目开发:集成flutter模块找不到自定义插件
我写了一个在纯flutter环境下可以正常运行的插件。然而,将flutter项目集成到iOS项目后,使用Xcode启动iOS项目并进入flutter界面。 ,但是报了如下错误:
Unhandled Exception: MissingPluginException(No implementation found for method getPlatformVersion on channel koolearn_test)
代码如下:
AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[ViewController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
ViewController:
#import ""
#import <Flutter/Flutter.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
FlutterViewController* flutterViewController = [[FlutterViewController alloc] init];
[self presentViewController:flutterViewController animated:false completion:nil];
}
第一次尝试:引入GenegedPluginRegistrant
创建插件工程时,目录自动生成一个示例文件其中包含测试项目补充。打开iOS测试项目,在AppDelegate类中我看到比我写的插件代码多了一行注册代码,所以我肯定是连接到iOS项目了。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[ViewController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
尝试结果:失败,仍然报上述错误
第二次尝试:将RootViewController设置为FlutterViewController
我在闲鱼博客上看到了一个解决方案,就是将RootViewController设置为FlutterViewController。测试正常,错误解决。但是我们项目的首页是原生页面,所以窗口的rootViewController不能是FlutterViewController。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 显示Window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:[[FlutterViewController alloc] initWithNibName:nil bundle:nil]];
[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window makeKeyAndVisible];
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
第三次尝试:引入FlutterEngine
终于在flutter wiki上找到了新的解决方案。创建一个新的FlutterEngine,并让FlutterEngine作为插件的注册对象。问题解决了。 ?
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。