iOS与flutter混编

一、创建iOS工程

如果你已经有iOS工程,可以直接使用。这里我们先创建一个空的iOS工程来模拟已有的工程,取名叫flutter_learning。

二、创建Flutter工程

进入iOS工程同一层的目录,假如你的项目是在...path1/path2/yourApp,那么你应该进入到path2目录中,即让Flutter工程与iOS工程在同一目录下。

$ cd /Users/mohaoyang/Desktop/flutter/项目    
$ flutter create -t module my_flutter  //创建名为my_flutter的flutter工程
创建flutter工程.png

三、创建/编辑Podfile文件

cd到iOS工程的根目录。执行以下命令:

$ pod init

编辑Podfile文件,添加以下两句话:

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

完整的Podfile文件:

# platform :ios, '9.0'
target 'flutter_learning' do

//添加以下代码
flutter_application_path = '../my_flutter'
    eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)  

end

然后执行:

$ pod install

执行成功:
执行成功.png

四、配置iOS工程

  • 配置Enable Bitcode为NO。
    由于flutter工程不支持bitcode,所以需要在xcode设置Build Settings->Build Options->Enable Bitcode为NO。
  • 配置 Run Script
    Build Phases-> +号 -> New Run Script Phase。展开新的Run Script。
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed
添加的shell脚本.png

注:务必保证Run Script在Target Dependencies phase后面

五、 在iOS项目中,通过“FlutterViewController”跳转至Flutter页面

参考如下代码,修改你的工程文件:

AppDelegate.h/m
  • AppDelegate.h
#import <UIKit/UIKit.h>
#import <Flutter/Flutter.h>

@interface AppDelegate : FlutterAppDelegate

@end
  • AppDelegate.m
#import "AppDelegate.h"
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h> // Only if you have Flutter Plugins

@interface AppDelegate () @end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [GeneratedPluginRegistrant registerWithRegistry:self];
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end
ViewController.h/m
  • ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end
  • ViewController.m
#import "ViewController.h"
#import <Flutter/Flutter.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(handleButtonAction)
     forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"跳转至flutter" 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 alloc] init];
    flutterViewController.view.backgroundColor = [UIColor cyanColor];
    [flutterViewController setInitialRoute:@"route1"];
    [self presentViewController:flutterViewController animated:YES completion:nil];
}

@end

六、热启动/热加载 与 Dart代码调试

  • 热启动/热加载
flutter attach    //执行此命令, 热重载
Checking for advertised Dart observatories...
Waiting for a connection from Flutter on iPhone Xʀ...
  • 点击按钮跳转至flutter页面出现:
Done.
Syncing files to device iPhone Xʀ...                             1,698ms

🔥  To hot reload changes while running, press "r". To hot restart (and rebuild state), press "R".
An Observatory debugger and profiler on iPhone Xʀ is available at: http://127.0.0.1:63543/4Gc5vgb1dg8=/
For a more detailed help message, press "h". To detach, press "d"; to quit, press "q".

至此,就已经完成了 在现有的iOS工程中接入Flutter 的所有流程~~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容