iOS项目集成FLutter

作者: YY_Lee | 来源:发表于2019-05-13 17:43 被阅读2次

本文是在FLutter环境已经搭建好的前提下,记录如何向iOS原生项目中集成Flutter。

  • 创建FLutter module
    打开终端 ,进入原生项目的上一级目录,如项目目录是:xxx/FlutterProject/FlutterHybridProject/
$ cd xxx/FlutterProject/
$ flutter create -t module my_flutter

以上命令运行后会在xxx/FlutterProject/生成一个flutter_module文件夹。文件夹内的内容如下图:


flutter_module.jpg

其中.iOS和.andriod分别是iOS和安卓的宿主工程,lib里面是dart部分的代码。flutter_module这个项目可以用AndroidStudio独立打开运行。

接下来向podfile中添加如下内容:

flutter_application_path = 'path/to/my_flutter/'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)

添加后的效果如下:


target 'FLutterHybridDemo' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for FLutterHybridDemo
  flutter_application_path = '../flutter_module/'
  eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)

  target 'FLutterHybridDemoTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'FLutterHybridDemoUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end

然后运行pod install,运行完成,pod中就集成了FLutter和FlutterPluginRegistrant。当我们在flutter_module/pubspec.yaml中添加FLutter插件后,除了需要运行flutter packages get,还需要在iOS目录下运行pod install,才能确保插件和flutter.framework添加到iOS项目中。

  • 禁用Bitcode
    目前flutter还不支持Bitcode,在Build Settings->Build Options->Enable Bitcode中将Bitcode设置为NO。

  • 在Build phase中添加Run Script,添加如下配置,并将Run Script移动至Target Dependencies phase下面,如下图:

RunScript.jpg

至此,就集成完毕了,编译不报错就可。

iOS项目中调用Futter module

iOS中有两种方式调用flutter模块:

  • 直接使用FlutterViewController
  • 使用FlutterEngine
使用FlutterViewController的方式

可通过调用setInitialRoute方法设置路由和传参。

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self
               action:@selector(handleButtonAction)
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Press me" forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor blueColor]];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.view addSubview:button];
}
    
- (void)handleButtonAction {
    //方式二
    FlutterViewController *flutterViewController = [FlutterViewController new];
    [flutterViewController setInitialRoute:@"{name:test,dataList:['11','22']}"];
    [self presentViewController:flutterViewController animated:YES completion:nil];
}
使用FlutterEngine的方式

AppDelegate.h中导入#import <Flutter/Flutter.h>,让AppDelegate集成FlutterAppDelegate,添加属性flutterEngine。

#import <UIKit/UIKit.h>
#import <Flutter/Flutter.h>

@interface AppDelegate : FlutterAppDelegate <UIApplicationDelegate>

//@property (strong, nonatomic) UIWindow *window;
@property(strong,nonatomic)FlutterEngine *flutterEngine;

@end

AppDelegate.m文件中导入#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>,做如下设置:

#import "AppDelegate.h"
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.flutterEngine = [[FlutterEngine alloc]initWithName:@"io.flutter" project:nil];
    [self.flutterEngine runWithEntrypoint:nil];
    [GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

原生跳转flutter方式如下:

- (void)handleButtonAction {
    // 方式一
    FlutterEngine  *flutterEngine = [(AppDelegate *)[[UIApplication sharedApplication] delegate] flutterEngine];
    FlutterViewController *flutterViewController = [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
    [flutterViewController setInitialRoute:@"{name:devio,dataList:['aa','bb']}"];
    [self presentViewController:flutterViewController animated:YES completion:nil];
}

相关文章

网友评论

    本文标题:iOS项目集成FLutter

    本文链接:https://www.haomeiwen.com/subject/axjraqtx.html