flutter與原生混編(iOS)

一、創(chuàng)建項目及配置
1,創(chuàng)建ios項目,同時在同一個根目錄下創(chuàng)建flutter項目
flutter項目使用命令flutter create -t module xxxflutter項目名
如圖


工程目錄.png

2,將flutter項目以pod的形式加入ios項目
2.1,如果項目之前沒有用過pod,則pod init創(chuàng)建podfile
如果使用過,直接進行步驟2.2。
2.2,podfile中添加

flutter_application_path = '../xxxflutter項目名'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

flutter_application_path是flutter項目的路徑
2.3,target中加入(一次性將flutter的編譯產(chǎn)物由此依賴進入iOS項目中,可跳過步驟3)

install_all_flutter_pods(flutter_application_path)

3,配置腳本(已完成步驟2.3可進行步驟4)
打開ios項目,在Build Phases中左上角添加Run Script


添加Run Script.png

Run Script內(nèi)容.png
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed 

4,設(shè)置bitcode
Flutter 目前還不支持 BitCode,需要設(shè)置為No


圖片.png

二、混編使以后添加的每個模塊都以package形式,在module中pubspec.yaml文件中引用(基于內(nèi)存消耗考慮)
1,頁面跳轉(zhuǎn)
原生處理:
flutter在iOS中的使用,主要是以FlutterViewController為載體,并在其內(nèi)部采用FlutterEngine對視圖進行渲染。
導(dǎo)入頭文件:

#import <Flutter/Flutter.h>
初始化FlutterEngine:
FlutterEngine *engine = [[FlutterEngine alloc] initWithName:@"xxx"];//xxx是唯一標(biāo)識
if ([engine run]) {
    self.flutterEngine = engine;
}
初始化flutter控制器
_flutterVC = [[FlutterViewController alloc] initWithEngine:self.flutterEngine nibName:nil bundle:nil];
跳轉(zhuǎn)
//“channelA”與flutter定義的名字保持一致
FlutterMethodChannel *methodChannel = [FlutterMethodChannel methodChannelWithName:@"channelA" binaryMessenger:self.flutterVC];
//使用指定的參數(shù)調(diào)用指定的Flutter方法,“methodA”與flutter定義的名字保持一致
[methodChannel invokeMethod:@"methodA" arguments:nil];
根據(jù)flutter定義的方法名做相應(yīng)的操作:在flutter頁面點擊按鈕跳轉(zhuǎn)至原生頁面
[methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
    if ([call.method isEqualToString:@"next"]) { //”next“為flutter定義的方法
        NativeViewController *vc = [[NativeViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}];
[self.navigationController pushViewController:self.flutterVC animated:YES]; //跳轉(zhuǎn)至flutter頁面

flutter處理:
在module項目的main里

導(dǎo)入頭文件
import 'package:flutter/services.dart';

定義channel,名字與原生保持一致

final MethodChannel _channelA = MethodChannel("methodA");
在init方法里添加在該channel上接收方法的回調(diào)
此處_pageIndex用來處理顯示flutter哪個頁面
_channelOne.setMethodCallHandler((call) {
  setState(() {
    _pageIndex = call.method;
  });
  return null;
});
flutter頁面點擊按鈕跳轉(zhuǎn)至原生頁面
onTap: () {
  MethodChannel(“channelA”).invokeMethod(“next”); 
  //channel名字和method名字需與原生保持一致
}

2,數(shù)據(jù)傳遞
二者之間的數(shù)據(jù)傳遞iOS使用FlutterBasicMessageChannel類,flutter使用BasicMessageChannel類,用法與MethodChannel類似
原生處理:

初始化channel
self.msgChannel = [FlutterBasicMessageChannel messageChannelWithName:@“messageChannel" binaryMessenger:self.flutterVC];//同樣 channel名字與flutter保持一致

接收數(shù)據(jù):

處理接收到的flutter數(shù)據(jù)
[self.msgChannel setMessageHandler:^(id  _Nullable message, FlutterReply  _Nonnull callback) {
    NSLog(@"收到了來自flutter的消息:%@",message);
}];

發(fā)送數(shù)據(jù):

在需要向flutter發(fā)送數(shù)據(jù)的地方調(diào)用
[self.msgChannel sendMessage:message];

flutter處理:

首先依然是導(dǎo)入頭文件
import 'package:flutter/services.dart';

初始化channel,channel名字與原生保持一致
final BasicMessageChannel _msgChannel =
 BasicMessageChannel("messageChannel", StandardMessageCodec());
接收數(shù)據(jù):
在init方法里添加在該channel上接收方法的回調(diào)
_msgChannel.setMessageHandler((message) {
  print("收到了來之iOS的消息:$message");
  return null;
});
發(fā)送數(shù)據(jù):在需要的地方之間調(diào)用send方法即可
_msgChannel.send(message);

問題:


問題.png

網(wǎng)上搜的方法,在podfile最前邊加eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)句話
執(zhí)行pod install失敗

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

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

target 'iosfix' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for iosfix
  install_all_flutter_pods(flutter_application_path)

  target 'iosfixTests' do
    inherit! :search_paths
    # Pods for testing
    install_all_flutter_pods(flutter_application_path)
  end

  target 'iosfixUITests' do
    # Pods for testing
    install_all_flutter_pods(flutter_application_path)
  end

end

于是又搜了下別的文章,添加的第二句話為load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb'),試了下成功了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容