本文將簡(jiǎn)單梳理一下 iOS 工程接入的 Flutter Boost 的流程,以作為前文的補(bǔ)充离赫。
參見(jiàn)前文:Flutter Boost 混合開(kāi)發(fā)實(shí)踐與源碼解析(以 Android 為例)四啰,F(xiàn)lutter Module 也依舊用前文生成的垂寥,目錄結(jié)構(gòu)依舊如前文所述耕突,不再贅述厂抽。
- 接入
2.1 工程準(zhǔn)備
準(zhǔn)備一個(gè)配有 Cocoapods 的空白工程浪秘,在 Podfile 中配置我們之前準(zhǔn)備好的 Flutter Module 作為依賴(lài):
flutter_application_path = '../flutter_module'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
target 'FlutterHybridiOS' do
install_all_flutter_pods(flutter_application_path)
end
接著在工程根目錄下運(yùn)行 pod install 蒋情,即可集成上 Flutter Module∷市看到我們的 Pods 中多了以下幾個(gè)模塊棵癣,即說(shuō)明集成成功。
接著在工程根目錄下運(yùn)行 pod install
夺衍,即可集成上 Flutter Module狈谊。看到我們的 Pods 中多了以下幾個(gè)模塊,即說(shuō)明集成成功河劝。
如不了解 Cocoapods壁榕,請(qǐng)參見(jiàn) CocoaPods 使用指南
2.2 實(shí)現(xiàn)路由類(lèi)
這一塊直接參照 Flutter Boost 官方提供的 example 就好了:
PlatformRouterImp.h:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <flutter_boost/FlutterBoost.h>
NS_ASSUME_NONNULL_BEGIN
@protocol FLBPlatform;
/**
* 實(shí)現(xiàn)平臺(tái)側(cè)的頁(yè)面打開(kāi)和關(guān)閉,不建議直接使用用于頁(yè)面打開(kāi)赎瞎,建議使用FlutterBoostPlugin中的open和close方法來(lái)打開(kāi)或關(guān)閉頁(yè)面牌里;
* FlutterBoostPlugin帶有頁(yè)面返回?cái)?shù)據(jù)的能力
*/
@interface PlatformRouterImp : NSObject<FLBPlatform>
@property (nonatomic,strong) UINavigationController *navigationController;
@end
NS_ASSUME_NONNULL_END
PlatformRouterImp.m:
#import "PlatformRouterImp.h"
#import <flutter_boost/FlutterBoost.h>
@interface PlatformRouterImp()
@end
@implementation PlatformRouterImp
#pragma mark - Boost 1.5
- (void)open:(NSString *)name
urlParams:(NSDictionary *)params
exts:(NSDictionary *)exts
completion:(void (^)(BOOL))completion
{
BOOL animated = [exts[@"animated"] boolValue];
FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
[vc setName:name params:params];
[self.navigationController pushViewController:vc animated:animated];
if(completion) completion(YES);
}
- (void)present:(NSString *)name
urlParams:(NSDictionary *)params
exts:(NSDictionary *)exts
completion:(void (^)(BOOL))completion
{
BOOL animated = [exts[@"animated"] boolValue];
FLBFlutterViewContainer *vc = FLBFlutterViewContainer.new;
[vc setName:name params:params];
[self.navigationController presentViewController:vc animated:animated completion:^{
if(completion) completion(YES);
}];
}
- (void)close:(NSString *)uid
result:(NSDictionary *)result
exts:(NSDictionary *)exts
completion:(void (^)(BOOL))completion
{
BOOL animated = [exts[@"animated"] boolValue];
animated = YES;
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
可以看到,F(xiàn)lutter Boost 支持常規(guī) push务甥,也支持打開(kāi)模態(tài)彈窗牡辽,也支持手動(dòng) pop。
2.3 綁定路由管理
AppDelegate.h:
#import <UIKit/UIKit.h>
#import <flutter_boost/FlutterBoost.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nullable, nonatomic, strong) UIWindow *window;
@end
AppDelegate.m:
#import "AppDelegate.h"
#import "PlatformRouterImp.h"
#import <flutter_boost/FlutterBoost.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
PlatformRouterImp *router = [PlatformRouterImp new];
[FlutterBoostPlugin.sharedInstance startFlutterWithPlatform:router
onStart:^(FlutterEngine *engine) {
}];
UITabBarController *tabVC = [[UITabBarController alloc] init];
UINavigationController *rvc = [[UINavigationController alloc] initWithRootViewController:tabVC];
router.navigationController = rvc;
return YES;
}
3. 使用
- (void)openClick:(UIButton *)button
{
[FlutterBoostPlugin open:@"first" urlParams:@{kPageCallBackId:@"MycallbackId#1"} exts:@{@"animated":@(YES)} onPageFinished:^(NSDictionary *result) {
NSLog(@"call me when page finished, and your result is:%@", result);
} completion:^(BOOL f) {
NSLog(@"page is opened");
}];
}
- (void)openPresentClick:(UIButton *)button
{
[FlutterBoostPlugin open:@"second" urlParams:@{@"present":@(YES),kPageCallBackId:@"MycallbackId#2"} exts:@{@"animated":@(YES)} onPageFinished:^(NSDictionary *result) {
NSLog(@"call me when page finished, and your result is:%@", result);
} completion:^(BOOL f) {
NSLog(@"page is presented");
}];
}
同樣的敞临,這里可在 Native 端用兩種不同的方式去打開(kāi)我們?cè)?Flutter Module 中注冊(cè)好的路由名态辛。
至此,我們成功在 iOS 工程中接入了 Flutter Boost哟绊,那就開(kāi)啟我們的混編之旅吧~