Flutter 混合開發FlutterBoost iOS 接入流程

緊接着上次的FlutterBoost Android版本接入,這次主要講iOS相關的接入

1.創建Flutter module

這個步驟前面的Android版本一樣

flutter create -t module flutter_module

2.iOS開始接入

2.1 Pod集成

現在一般的iOS應用都是用cocopod集成的,一般都有對應的Podfile文件,在對應的Podfile文件末尾處加入以下代碼

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

最好也和Android一樣,分開兩個工程,iOS工程和flutter功能是平級的,這樣互不影響

之後再iOS工程目錄下執行 pod install 命令,會在pod下面的Development Pods文件下面看到Flutter 和FlutterPluginRegistrant 兩個文件。

如果出現啥錯誤,記得在工程的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填入以上腳本

之後執行Build 編譯,項目應該能運行起來,如果出現執行上面的步驟。

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的業務跳轉

#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

這樣即可點擊跳轉到Flutter默認生成的main界面

4.FlutterBoost接入

4.1 Flutter 工程接入FlutterBoost

在對應的pubspec.yaml文件中加入依賴,pubspec.yaml就是一個配置文件

flutter_boost:
        git:
            url: 'https://github.com/alibaba/flutter_boost.git'
            ref: '0.0.411'

之後調用Package get,右上角即可查看,之後還是在命令行工具下在flutte_module 根目下,執行flutter build ios 以及在iOS的根目錄下執行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 ,而對應的.m文件可保持不變或者去掉

[GeneratedPluginRegistrant registerWithRegistry:self];

都可以

4.3.3 實現FLBPlatform協議

應用程序實現FLBPlatform協議方法,可以使用官方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

也可以自己根據此修改。其中的openPage 方法會接收來至flutter–>native以及native–>flutter的頁面跳轉,可以根據用戶自由的書寫

4.3.5 初始化FlutterBoost

[FlutterBoostPlugin.sharedInstance startFlutterWithPlatform:router
                                                        onStart:^(FlutterViewController * fvc){
                                                            
                                                        }];

官方demo是在AppDelegate中初始化的,可以修改FLBPlatform協議實現類完成對應的操作對應的初始化做

5.頁面跳轉

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);

最終都會跳轉到FLBPlatform 協議實現類的openPage 方法中,很多操作都是在FLBPlatform協議實現類中,包括頁面跳轉,關閉,以及對應的一些Flutter 和Native通信相關的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章