写了一个插件,在纯flutter环境下可以正常运行,可是在将flutter项目集成到iOS项目中,用Xcode启动iOS项目,进入flutter界面,却报以下错误:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10470.html
Unhandled Exception: MissingPluginException(No implementation found for method getPlatformVersion on channel koolearn_test)
代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10470.html
AppDelegate:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10470.html
- (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:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10470.html
#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];
}
第一次尝试:引入GeneratedPluginRegistrant
在创建插件工程的时候,flutter插件目录下会自动生成一个example文件,里面是插件的测试工程。打开iOS测试工程,在AppDelegate类中,发现比我写的插件代码多了一行注册代码,果断加入iOS项目。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10470.html
- (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];
}
尝试结果:失败,仍旧报上面错误文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10470.html
第二次尝试:将RootViewController设置为FlutterViewController
在咸鱼的博客上看到一个解决方法,就是把RootViewController设置为FlutterViewController。测试OK,错误解决。但是我们的工程首页是Native页面,所以Window的rootViewController不能是FlutterViewController。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10470.html
- (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作为插件的注册对象,问题解决。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10470.html
AppDelegate:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10470.html
#import <UIKit/UIKit.h>
#import <Flutter/Flutter.h>
@interface AppDelegate : FlutterAppDelegate
@property (nonatomic,strong) FlutterEngine *flutterEngine;
@end
@implementation 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];
= [[FlutterEngine alloc] initWithName:@"" project:nil];
[ runWithEntrypoint:nil];
[GeneratedPluginRegistrant registerWithRegistry:];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end
ViewController:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10470.html
#import ""
#import <Flutter/Flutter.h>
#import ""
@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 {
FlutterEngine *flutterEngine = [(AppDelegate *)[[UIApplication sharedApplication] delegate] flutterEngine];
FlutterViewController *flutterViewController = [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
[self presentViewController:flutterViewController animated:false completion:nil];
}
@end
碰到错误
运行Xcode工程,如果报以下错误,那么对工程进行pod install 或 pod update文章源自菜鸟学院-https://www.cainiaoxueyuan.com/xcx/10470.html
2019-07-02 14:39 1F
很棒。。我也卡在这里了
感谢分享