本文是在FLutter環(huán)境已經(jīng)搭建好的前提下乘凸,記錄如何向iOS原生項(xiàng)目中集成Flutter厕诡。
- 創(chuàng)建FLutter module
打開終端 ,進(jìn)入原生項(xiàng)目的上一級(jí)目錄营勤,如項(xiàng)目目錄是:xxx/HybridTest/HybridProject/
$ cd xxx/HybridTest/
$ flutter create -t module my_flutter
以上命令運(yùn)行后會(huì)在xxx/HybridTest/下生成一個(gè)名為my_flutter文件夾灵嫌。文件夾內(nèi)的內(nèi)容如下圖(如果你沒有打開顯示隱藏文件,打開終端輸入命令:defaults write com.apple.finder AppleShowAllFiles -boolean true;killall Finder葛作;再次隱藏文件寿羞,輸入命令:defaults write com.apple.finder AppleShowAllFiles -boolean false;killall Finder):
其中.iOS和.andriod分別是iOS和安卓的宿主工程,lib里面是dart部分的代碼赂蠢。my_flutter這個(gè)項(xiàng)目可以用AndroidStudio獨(dú)立打開運(yùn)行绪穆。
接下來向podfile中添加如下內(nèi)容:
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環(huán)境,按照上述設(shè)置podfile虱岂,執(zhí)行pod install后出現(xiàn)如下情況:
出現(xiàn)這種情況按照下圖設(shè)置podfile即可解決:
參見文檔Add Flutter to existing apps
然后運(yùn)行pod install玖院,運(yùn)行完成,pod中就集成了FLutter和FlutterPluginRegistrant第岖。當(dāng)我們?cè)趂lutter_module/pubspec.yaml中添加FLutter插件后难菌,除了需要運(yùn)行flutter packages get,還需要在iOS目錄下運(yùn)行pod install蔑滓,才能確保插件和flutter.framework添加到iOS項(xiàng)目中郊酒。
禁用Bitcode
目前flutter還不支持Bitcode,在Build Settings->Build Options->Enable Bitcode中將Bitcode設(shè)置為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移動(dòng)至Target Dependencies phase下面蹄咖,如下圖:
至此荠耽,就集成完畢了,編譯不報(bào)錯(cuò)就可比藻。
iOS項(xiàng)目中調(diào)用Futter module
iOS中有兩種方式調(diào)用flutter模塊:
- 直接使用FlutterViewController
- 使用FlutterEngine
使用FlutterViewController的方式
可通過調(diào)用setInitialRoute方法設(shè)置路由和傳參铝量。
- (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中導(dǎo)入#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文件中導(dǎo)入#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>,做如下設(shè)置:
#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];
}
原生跳轉(zhuǎn)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];
}