緊接著上次的FlutterBoost Android版本接入竭钝,這次主要講iOS相關(guān)的接入
1.創(chuàng)建Flutter module
這個步驟前面的Android版本一樣
flutter create -t module flutter_module
2.iOS開始接入
2.1 Pod集成
現(xiàn)在一般的iOS應(yīng)用都是用cocopod集成的梨撞,一般都有對應(yīng)的Podfile文件,在對應(yīng)的Podfile文件末尾處加入以下代碼
flutter_application_path = '../flutter_module'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)
最好也和Android一樣香罐,分開兩個工程卧波,iOS工程和flutter功能是平級的,這樣互不影響
之后再iOS工程目錄下執(zhí)行 pod install 命令庇茫,會在pod下面的Development Pods文件下面看到Flutter 和FlutterPluginRegistrant 兩個文件幽勒。
如果出現(xiàn)啥錯誤,記得在工程的BuildSettings 下面檢查Enable BitCode是否為NO港令。
2.2添加編譯腳本
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed
在BuildPhases 欄下啥容,點擊左上角的加號(+) 選擇New Run Script Phase填入以上腳本
之后執(zhí)行Build 編譯锈颗,項目應(yīng)該能運行起來,如果出現(xiàn)執(zhí)行上面的步驟咪惠。
3.混編代碼集成
修改AppDelegate.h/m文件
#import <UIKit/UIKit.h>
#import <Flutter/Flutter.h>
@interface AppDelegate : FlutterAppDelegate <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
+ (AppDelegate *)sharedAppDelegate;
@end
h頭文件需要集成FlutterAppDelegate
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>
#import "AppDelegate.h"
#import "AppDelegate+Init.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self initConfigWithOptions:launchOptions];
[self.window makeKeyAndVisible];
[GeneratedPluginRegistrant registerWithRegistry:self];
return YES;
}
在AppDelegate.m文件的didFinishLaunchingWithOptions方法中加入插件集成的方法
[GeneratedPluginRegistrant registerWithRegistry:self];
增加ViewController的業(yè)務(wù)跳轉(zhuǎn)
#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:@"點我" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor redColor]];
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
這樣即可點擊跳轉(zhuǎn)到Flutter默認生成的main界面
4.FlutterBoost接入
4.1 Flutter 工程接入FlutterBoost
在對應(yīng)的pubspec.yaml文件中加入依賴击吱,pubspec.yaml就是一個配置文件
flutter_boost:
git:
url: 'https://github.com/alibaba/flutter_boost.git'
ref: '0.0.411'
之后調(diào)用Package get,右上角即可查看遥昧,之后還是在命令行工具下在flutte_module 根目下覆醇,執(zhí)行flutter build ios 以及在iOS的根目錄下執(zhí)行pod install 使iOS和flutter都添加FlutterBoost插件。
4.2Flutter中main.dart文件中配置
可以參考前面的Android版本
4.3 iOS工程的修改
4.3.1 添加libc++
需要 將libc++ 加入 "Linked Frameworks and Libraries"
這個主要是項目的General 的Linked Frameworks and Libraries 欄下炭臭,點擊加號(+)搜索libc++,找到libc++.tbd即可
4.3.2 修改AppDelegate.h/m文件
#import <UIKit/UIKit.h>
#import <flutter_boost/FlutterBoost.h>
@interface AppDelegate : FLBFlutterAppDelegate <UIApplicationDelegate, XGPushDelegate>
@property (strong, nonatomic) UIWindow *window;
+ (AppDelegate *)sharedAppDelegate;
@end
需要繼承FLBFlutterAppDelegate 永脓,而對應(yīng)的.m文件可保持不變或者去掉
[GeneratedPluginRegistrant registerWithRegistry:self];
都可以
4.3.3 實現(xiàn)FLBPlatform協(xié)議
應(yīng)用程序?qū)崿F(xiàn)FLBPlatform協(xié)議方法,可以使用官方demo中的DemoRouter
@interface DemoRouter : NSObject<FLBPlatform>
@property (nonatomic,strong) UINavigationController *navigationController;
+ (DemoRouter *)sharedRouter;
@end
@implementation DemoRouter
- (void)openPage:(NSString *)name
params:(NSDictionary *)params
animated:(BOOL)animated
completion:(void (^)(BOOL))completion
{
if([params[@"present"] boolValue]){
FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
[vc setName:name params:params];
[self.navigationController presentViewController:vc animated:animated completion:^{}];
}else{
FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
[vc setName:name params:params];
[self.navigationController pushViewController:vc animated:animated];
}
}
- (void)closePage:(NSString *)uid animated:(BOOL)animated params:(NSDictionary *)params completion:(void (^)(BOOL))completion
{
FLBFlutterViewContainer *vc = (id)self.navigationController.presentedViewController;
if([vc isKindOfClass:FLBFlutterViewContainer.class] && [vc.uniqueIDString isEqual: uid]){
[vc dismissViewControllerAnimated:animated completion:^{}];
}else{
[self.navigationController popViewControllerAnimated:animated];
}
}
@end
也可以自己根據(jù)此修改鞋仍。其中的openPage 方法會接收來至flutter-->native以及native-->flutter的頁面跳轉(zhuǎn)常摧,可以根據(jù)用戶自由的書寫
4.3.5 初始化FlutterBoost
[FlutterBoostPlugin.sharedInstance startFlutterWithPlatform:router
onStart:^(FlutterViewController * fvc){
}];
官方demo是在AppDelegate中初始化的,可以修改FLBPlatform協(xié)議實現(xiàn)類完成對應(yīng)的操作對應(yīng)的初始化做
5.頁面跳轉(zhuǎn)
Native-->Flutter
FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
[vc setName:name params:params];
[self.navigationController presentViewController:vc animated:animated completion:^{}];
Flutter-->Native
FlutterBoost.singleton.openPage("pagename", {}, true);
最終都會跳轉(zhuǎn)到FLBPlatform 協(xié)議實現(xiàn)類的openPage 方法中威创,很多操作都是在FLBPlatform協(xié)議實現(xiàn)類中落午,包括頁面跳轉(zhuǎn),關(guān)閉肚豺,以及對應(yīng)的一些Flutter 和Native通信相關(guān)的实辑。
版本升級1.9.1
iOS的pod文件需要修改
flutter_application_path = 'path/to/my_flutter/'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
target 'MyApp' do
install_all_flutter_pods(flutter_application_path)
end
target 'MyAppTests' do
install_all_flutter_pods(flutter_application_path)
end
并且需要去掉下面的腳本
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed
具體詳情可以查看GitHub Flutter官方文檔