Flutter 混合開發FlutterBoost iOS 接入流程

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

1.建立Flutter module

這個步驟前面的Android版本同樣c++

flutter create -t module flutter_module
複製代碼

2.iOS開始接入

2.1 Pod集成

如今通常的iOS應用都是用cocopod集成的,通常都有對應的Podfile文件,在對應的Podfile文件末尾處加入如下代碼git

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

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

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

若是出現啥錯誤,記得在工程的BuildSettings 下面檢查Enable BitCode是否爲NO。bash

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填入以上腳本app

以後執行Build 編譯,項目應該能運行起來,若是出現執行上面的步驟。ide

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頭文件須要集成FlutterAppDelegateui

#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通訊相關的。

相關文章
相關標籤/搜索