官方混编文档:https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps#ios
一、配置过程
-
1. 新建一个xcode工程:
FlutterNativeFrame
跟flutter在同级别路径
2. binding flutter
// flutter_application_path:flutter工程的绝对路径
flutter_application_path = '**/**/my_flutter'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)
-
3.执行
pod install
执行完之后,打开xcode工程,出现如图所示的两个文件夹,success
-
4. 添加脚本文件
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed
ok,run,如果运行报错,把Enable Bitcode
设为NO
,Flutter混合开发还不支持bit code,所以在iOS工程检查项目并关闭bit code
现在应该可以运行起来了,如果还是不行,重复以上步骤
混编开始:
修改AppDelegate.h、AppDelegate.m
// .h
#import <UIKit/UIKit.h>
#import <Flutter/Flutter.h>
@interface AppDelegate : FlutterAppDelegate
@end
// .m
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h> // Only if you have Flutter Plugins
#include "AppDelegate.h"
@implementation AppDelegate
// This override can be omitted if you do not have any Flutter Plugins.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end
ViewController跳转
#import <Flutter/Flutter.h>
#import "ViewController.h"
@implementation ViewController
- (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 alloc] init];
[self presentViewController:flutterViewController animated:false completion:nil];
}
@end
二、热更新 Hot Reload
cd到flutter工程路径,执行flutter attach
attach成功之后,运行xcode,更新直接press 'r'
在VStudio中修改dart文件,press 'r'直接可以看到修改之后的显示
官网链接:Hot reload https://flutter.io/docs/development/tools/hot-reload
官方混编文档:https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps#ios