iOS项目集成FLutter

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

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

以上命令运行后会在xxx/HybridTest/下生成一个名为my_flutter文件夹。文件夹内的内容如下图(如果你没有打开显示隐藏文件,打开终端输入命令:defaults write com.apple.finder AppleShowAllFiles -boolean true;killall Finder;再次隐藏文件,输入命令:defaults write com.apple.finder AppleShowAllFiles -boolean false;killall Finder):

my_flutter.jpg

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

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

flutter_application_path = '../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 = '../my_flutter/'
  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

PS:最近朋友使用V1.9版本的flutter环境,按照上述设置podfile,执行pod install后出现如下情况:


dependencies.jpeg

出现这种情况按照下图设置podfile即可解决:


podfile.jpeg

参见文档Add Flutter to existing apps

然后运行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,添加如下配置,

"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed

并将Run Script移动至Target Dependencies phase下面,如下图:

RunScript.png

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

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];
}

推荐阅读更多精彩内容