看了很多文章,发现都是集成的老版本FlutterBoost,一些api已经变更,然后又去官网看demo,最终成功集成 FlutterBoost ^1.12.13。
Flutter各版本对应的FlutterBoost请参考 https://github.com/alibaba/flutter_boost/blob/master/README_CN.md
1.创建flutter项目,在终端iOS工程目录同级执行
flutter create -t module flutter_module
2.在pubspec.yaml里添加flutter_boost的依赖
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.3
flutter_boost: ^1.12.13
3.终端运行:flutter packages get或在IDE里点击packages get
4.flutter项目的main.dart 代码是官方demo的
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'simple_page_widgets.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State {
@override
void initState() {
super.initState();
FlutterBoost.singleton.registerPageBuilders({
'first': (pageName, params, _) => FirstRouteWidget(),
'second': (pageName, params, _) => SecondRouteWidget(),
'tab': (pageName, params, _) => TabRouteWidget(),
'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params),
///可以在native层通过 getContainerParams 来传递参数
'flutterPage': (pageName, params, _) {
print("flutterPage params:$params");
return FlutterRouteWidget();
},
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Boost example',
builder: FlutterBoost.init(postPush: _onRoutePushed),
home: Container());
}
void _onRoutePushed(
String pageName, String uniqueId, Map params, Route route, Future _) {
}
}
主要是 FlutterBoost.singleton.registerPageBuilders 和 FlutterBoost.init(postPush: _onRoutePushed)
其他demo页面的代码请去https://github.com/alibaba/flutter_boost example_swift中查看
5.回到iOS工程,添加flutter_boost
修改podfile,里面添加下面代码
target '*****' do
......
use_frameworks!
......
flutter_application_path = "../flutter_module"
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
install_all_flutter_pods(flutter_application_path)
......
end
然后执行pod install
6.在AppDelegate.swift里让
import Flutter
import FlutterPluginRegistrant //看自己用不用
import flutter_boost
class AppDelegate: FlutterAppDelegate {
......
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FlutterBoostPlugin.sharedInstance().startFlutter(with: FlutterRouter()) { (flutterEngine) in
DDLogInfo("####### flutterEngine #########")
}
}
}
FlutterRouter是一个实现FLBPlatform协议的类:
class FlutterRouter : NSObject, FLBPlatform{
//里面实现FLBPlatform协议的方法,具体代码可以参考官方demo自行修改
func open(_ url: String, urlParams: [AnyHashable : Any], exts: [AnyHashable : Any], completion: @escaping (Bool) -> Void) {
var animated = false;
if exts["animated"] != nil{
animated = exts["animated"] as! Bool;
}
let vc = FLBFlutterViewContainer.init();
vc.setName(url, params: urlParams);
self.navigationController().pushViewController(vc, animated: animated);
completion(true);
}
func present(_ url: String, urlParams: [AnyHashable : Any], exts: [AnyHashable : Any], completion: @escaping (Bool) -> Void) {
var animated = false;
if exts["animated"] != nil{
animated = exts["animated"] as! Bool;
}
let vc = FLBFlutterViewContainer.init();
vc.setName(url, params: urlParams);
navigationController().present(vc, animated: animated) {
completion(true);
};
}
func close(_ uid: String, result: [AnyHashable : Any], exts: [AnyHashable : Any], completion: @escaping (Bool) -> Void) {
var animated = false;
if exts["animated"] != nil{
animated = exts["animated"] as! Bool;
}
let presentedVC = self.navigationController().presentedViewController;
let vc = presentedVC as? FLBFlutterViewContainer;
if vc?.uniqueIDString() == uid {
vc?.dismiss(animated: animated, completion: {
completion(true);
});
}else{
self.navigationController().popViewController(animated: animated);
}
}
func navigationController() -> UINavigationController {
let delegate = UIApplication.shared.delegate as! AppDelegate
let navigationController = delegate.window?.rootViewController as! UINavigationController
return navigationController;
}
}
在需要用到Flutter的地方调用FlutterBoostPlugin里的方法,会执行上面协议里的方法,最终实现跳转
FlutterBoostPlugin.open("first", urlParams: ["hello":"world"], exts: [:], onPageFinished: { (map) in
}) { (finishde) in
}
7.运行前需要先打包fluuer项目,可以在Build Phases里添加运行脚本
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed
在Target->Build Settings里添加自定义$FLUTTER_ROOT
FLUTTER_ROOT的值为FlutterSDK的目录路径
8.设置项目的Enable Bitcode 为 No
9.要添加libc++
10.运行iOS项目
步骤如果有遗漏,请大佬们提醒........