在 Flutter項(xiàng)目中集成完 flutter boost,并且已經(jīng)使用了 flutter boost進(jìn)行了路由管理,這時(shí)如果需要和iOS混合開發(fā),這時(shí)就要到 原生端進(jìn)行集成。
注意:之前建的項(xiàng)目必須是 Flutter module
項(xiàng)目,并且原生項(xiàng)目和flutter module項(xiàng)目在同一個(gè)文件夾下面
下面是原生端集成 flutter boost的步驟:
- 在原生項(xiàng)目的 Podfile文件中添加如下代碼
# Uncomment the next line to define a global platform for your project
platform :ios, '12.0'
flutter_application_path = '../my_flutter'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
target 'FlutterList' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for FlutterList
install_all_flutter_pods(flutter_application_path)
pod 'Masonry', '1.0.2'
end
post_install do |installer|
flutter_post_install(installer) if defined?(flutter_post_install)
end
填寫完后指向 pod install
。此時(shí)項(xiàng)目的pod目錄下面就會(huì)出現(xiàn) flutter相關(guān)的庫
到此就完成 flutter混合開發(fā)的集成工作,接下來就是需要 編寫使用代碼
-
編寫混合開發(fā)代碼
這里沒有跟著flutter boost 官網(wǎng)進(jìn)行集成 https://github.com/alibaba/flutter_boost/blob/master/docs/install.md 創(chuàng)建代碼,稍微進(jìn)行了些改進(jìn)。 -
HYFlutterBoostDelegate
-
HYFlutterViewContainer
-
HYFlutterViewController
分別創(chuàng)建了以上代碼,并且在AppDelegate 中使用FlutterBoost
。 -
HYFlutterBoostDelegate
import Foundation
import flutter_boost
class HYFlutterBoostDelegate: NSObject, FlutterBoostDelegate {
///您用來push的導(dǎo)航欄
var navigationController:UINavigationController? {
return UINavigationController.topNavigationController()?.navigationController
}
///用來存返回flutter側(cè)返回結(jié)果的表
var resultTable:Dictionary<String,([AnyHashable:Any]?)->Void> = [:];
func pushNativeRoute(_ pageName: String!, arguments: [AnyHashable : Any]!) {
//可以用參數(shù)來控制是push還是pop
let isPresent = arguments["isPresent"] as? Bool ?? false
let isAnimated = arguments["isAnimated"] as? Bool ?? true
//這里根據(jù)pageName來判斷生成哪個(gè)vc,這里給個(gè)默認(rèn)的了
let targetViewController = UIViewController()
// 這里也可以使用路由進(jìn)行跳轉(zhuǎn)
if(isPresent){
self.navigationController?.present(targetViewController, animated: isAnimated, completion: nil)
}else{
self.navigationController?.pushViewController(targetViewController, animated: isAnimated)
}
}
func pushFlutterRoute(_ options: FlutterBoostRouteOptions!) {
let vc:HYFlutterViewController = HYFlutterViewController()
vc.setName(options.pageName, uniqueId: options.uniqueId, params: options.arguments,opaque: options.opaque)
vc.hidesBottomBarWhenPushed = true
//對(duì)這個(gè)頁面設(shè)置結(jié)果
resultTable[options.pageName] = options.onPageFinished;
if let nav = navigationController {
nav.pushViewController(vc, animated: true)
}
}
func popRoute(_ options: FlutterBoostRouteOptions!) {
//如果當(dāng)前被present的vc是container,那么就執(zhí)行dismiss邏輯
if let vc = self.navigationController?.presentedViewController as? HYFlutterViewController, vc.uniqueIDString() == options.uniqueId{
//這里分為兩種情況,由于UIModalPresentationOverFullScreen下,生命周期顯示會(huì)有問題
//所以需要手動(dòng)調(diào)用的場景,從而使下面底部的vc調(diào)用viewAppear相關(guān)邏輯
if vc.modalPresentationStyle == .overFullScreen {
//這里手動(dòng)beginAppearanceTransition觸發(fā)頁面生命周期
self.navigationController?.topViewController?.beginAppearanceTransition(true, animated: false)
vc.dismiss(animated: true) {
self.navigationController?.topViewController?.endAppearanceTransition()
}
}else{
//正常場景,直接dismiss
vc.dismiss(animated: true, completion: nil)
}
}else{
self.navigationController?.popViewController(animated: true)
}
//否則直接執(zhí)行pop邏輯
//這里在pop的時(shí)候?qū)?shù)帶出,并且從結(jié)果表中移除
if let onPageFinshed = resultTable[options.pageName] {
onPageFinshed(options.arguments)
resultTable.removeValue(forKey: options.pageName)
}
}
}
- HYFlutterViewContainer
#import <flutter_boost/FlutterBoost.h>
NS_ASSUME_NONNULL_BEGIN
@interface HYFlutterViewContainer : FBFlutterViewContainer
@end
NS_ASSUME_NONNULL_END
#import "HYFlutterViewContainer.h"
@interface HYFlutterViewContainer (){
UINavigationBar *_bar;
}
@property (nonatomic)BOOL navigationBarHidden;
@property (nonatomic, strong) FBVoidCallback removeEventCallback;
@end
@implementation HYFlutterViewContainer
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
/// 設(shè)置這個(gè)container對(duì)應(yīng)的從flutter過來的事件監(jiān)聽
-(void)setupEventListeningFromFlutter{
__weak typeof(self) weakSelf = self;
// 為這個(gè)容器注冊監(jiān)聽,監(jiān)聽內(nèi)部的flutterPage往這個(gè)容器發(fā)的事件
self.removeEventCallback = [FlutterBoost.instance addEventListener:^(NSString *name, NSDictionary *arguments) {
__strong typeof(self) strongSelf = weakSelf;
//事件名
NSString *event = arguments[@"event"];
//事件參數(shù)
NSDictionary *args = arguments[@"args"];
if ([event isEqualToString:@"enablePopGesture"]) {
// 多page情況下的側(cè)滑動(dòng)態(tài)禁用和啟用事件
NSNumber *enableNum = args[@"enable"];
BOOL enable = [enableNum boolValue];
//右滑控制
// strongSelf.fd_interactivePopDisabled = !enable;
}
} forName:self.uniqueId];
}
- (BOOL)navigationBarHidden {
return YES;
}
- (UINavigationBar *)navBar
{
if (!_bar) {
_bar = [UINavigationBar new];
}
return _bar;
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
@end
- HYFlutterViewController
#import <UIKit/UIKit.h>
#import "HYFlutterViewContainer.h"
@interface HYFlutterViewController : UIViewController
@property (nonatomic, strong) HYFlutterViewContainer *container;
- (NSString *)uniqueIDString;
- (void)setName:(NSString *)name uniqueId:(NSString *)uniqueId params:(NSDictionary *)params opaque:(BOOL) opaque;
@end
#import "HYFlutterViewController.h"
#import <Masonry/Masonry.h>
#import "UINavigationController+HY.h"
@interface HYFlutterViewController ()
@end
@implementation HYFlutterViewController
- (void)setName:(NSString *)name uniqueId:(NSString *)uniqueId params:(NSDictionary *)params opaque:(BOOL) opaque {
_container = [HYFlutterViewContainer new];
[_container setName:name uniqueId:uniqueId params:params opaque:opaque];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 隱藏導(dǎo)航欄
[self.container.navigationController setNavigationBarHidden:YES animated:YES];
[self addChildViewController:_container];
[_container didMoveToParentViewController:self];
[self.view addSubview:_container.view];
[_container.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(UIEdgeInsetsZero);
}];
}
- (NSString *)uniqueIDString {
return self.container.uniqueIDString;
}
- (void)dealloc {
[_container removeFromParentViewController];
[_container didMoveToParentViewController:nil];
}
@end
- AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
HYFlutterBoostDelegate* delegate = [[HYFlutterBoostDelegate alloc]init];
[FlutterBoost.instance setup:application delegate:delegate callback:^(FlutterEngine *engine) {
NSLog(@"FlutterBoost 開始操作");
}];
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
ViewController* VC = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:VC];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
使用 Flutter boost進(jìn)行調(diào)轉(zhuǎn)文章來源:http://www.zghlxwxcb.cn/news/detail-638181.html
- (void)btnClick:(UIButton *)btn {
FlutterBoostRouteOptions* option = [[FlutterBoostRouteOptions alloc]init];
option.pageName = @"/";
[[[FlutterBoost instance] plugin].delegate pushFlutterRoute:option];
}
到此flutter boost原生交互使用結(jié)束,此時(shí)進(jìn)到flutter界面導(dǎo)航是沒有左側(cè)的返回按鈕的,這需要自己處理。文章來源地址http://www.zghlxwxcb.cn/news/detail-638181.html
到了這里,關(guān)于Flutter iOS 集成使用 flutter boost的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!