為您解碼網(wǎng)站建設(shè)的點點滴滴
發(fā)表日期:2018-10 文章編輯:小燈 瀏覽次數(shù):1732
Flutter 是一個很有潛力的框架,但是目前使用Flutter的APP并不算很多,相關(guān)資料并不豐富,介紹現(xiàn)有工程引入Flutter的相關(guān)文章也比較少。項目從零開始,引入Flutter操作比較簡單,但是現(xiàn)有工程引入Flutter 需要費(fèi)很多精力和時間,這里是我在完成現(xiàn)有iOS工程引入Flutter后寫的一次總結(jié)文章。
首先是要搭建Flutter環(huán)境,之前也寫了一篇相關(guān)文章搭建Flutter-iOS開發(fā)環(huán)境,可以參考一下
可以去官網(wǎng)查看:https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps
比較簡單,這里不做贅述。
首先建立flutter module,主要是用于獲取改flutter app中的Generated.xcconfig和framework
cd some/path/ $ flutter create -t module my_flutter
也可以用
flutter create app
建立flutter app,flutter app中也有Generated.xcconfig和framework
根據(jù)官網(wǎng),需要在工程中建立三個配置文件:
Flutter.xcconfig
、Debug.xcconfig
、Release.xcconfig
在XCode工程對應(yīng)目錄,右擊,選擇新建文件(New File),選中創(chuàng)建xcconfig文件,如圖:
Flutter.xcconfig
中填寫: //這里填寫前面建立的flutter module 的Generated.xcconfig的路徑 #include "../../my_flutter/.ios/Flutter/Generated.xcconfig" ENABLE_BITCODE=NO
Debug.xcconfig
中填寫:#include "../Flutter/Flutter.xcconfig"
在 Release.xcconfig
中填寫:
#include "../Flutter/Flutter.xcconfig" FLUTTER_BUILD_MODE=release
如果工程中用cocoapods管理,需要在 Debug.xcconfig
和Release.xcconfig
添加pod的路徑:
例如 Release.xcconfig
中
#include "Flutter.xcconfig" #include "工程路徑/Pods/Target Support Files/******.release.xcconfig"http://pod路徑 FLUTTER_BUILD_MODE=release
在準(zhǔn)備好這些xcconfig文件后,需要到XCode工程PROJECT(注意是PROJECT,不是Target)中的Configuration選項里,將對應(yīng)的target選擇成前面的xcconfig文件,Debug用Debug.xcconfig, Release用 Release.xcconfig
在工程的Build Phase中新建一個Run Script
,用于編譯時運(yùn)行腳本,
建立方法如圖:
建立Run Script
后,需要移動其對應(yīng)的位置,需要在Target dependencies
之后,如果用cocoapods管理工程需要在,Check Pods Manifest.lock
之后:
在腳本框中,填入以下代碼,用于引進(jìn)Flutter中的xcode_backend腳本:
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
如圖:
完成前面的配置后,便可以在XCode對工程進(jìn)行編譯build (Command+B),在提示“ Build Success ” 后,在iOS工程文件夾中會生成一個Flutter文件夾,將其加入工程目錄中,建議和剛才xcconfig所在目錄并列,
右鍵項目目錄 ,選擇 Add Files to 'xxx' ,Options選Create groups
,添加編譯生成的Flutter文件夾。需要注意但是:Flutter目錄下有個flutter_assets
文件,不能使用Create groups
的方式添加,只能用Creat folder references
的Options, 否則Flutter頁面會空白渲染不出來??梢詣h了flutter_assets在用Creat folder reference
s重新添加。
在添加完Flutter 文件夾之后,去Embeded Binaries
中添加App.framework
和Flutter.framework
Flutter需要和APP進(jìn)行交互,需要對AppDelegate 進(jìn)行改造:
AppDelegate.h文件中:
#import <Flutter/Flutter.h>@interface AppDelegate : FlutterAppDelegate <UIApplicationDelegate, FlutterAppLifeCycleProvider>@end
AppDelegate.m 文件中:
#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate { FlutterPluginAppLifeCycleDelegate *_lifeCycleDelegate; }- (instancetype)init { if (self = [super init]) { _lifeCycleDelegate = [[FlutterPluginAppLifeCycleDelegate alloc] init]; } return self; }- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { return [_lifeCycleDelegate application:application didFinishLaunchingWithOptions:launchOptions]; }- (void)applicationDidEnterBackground:(UIApplication*)application { [_lifeCycleDelegate applicationDidEnterBackground:application]; }- (void)applicationWillEnterForeground:(UIApplication*)application { [_lifeCycleDelegate applicationWillEnterForeground:application]; }- (void)applicationWillResignActive:(UIApplication*)application { [_lifeCycleDelegate applicationWillResignActive:application]; }- (void)applicationDidBecomeActive:(UIApplication*)application { [_lifeCycleDelegate applicationDidBecomeActive:application]; }- (void)applicationWillTerminate:(UIApplication*)application { [_lifeCycleDelegate applicationWillTerminate:application]; }- (void)application:(UIApplication*)application didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings { [_lifeCycleDelegate application:application didRegisterUserNotificationSettings:notificationSettings]; }- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { [_lifeCycleDelegate application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; }- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { [_lifeCycleDelegate application:applicationdidReceiveRemoteNotification:userInfofetchCompletionHandler:completionHandler]; }- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id>*)options { return [_lifeCycleDelegate application:application openURL:url options:options]; }- (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url { return [_lifeCycleDelegate application:application handleOpenURL:url]; }- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplicationannotation:(id)annotation { return [_lifeCycleDelegate application:applicationopenURL:urlsourceApplication:sourceApplication annotation:annotation]; }- (void)application:(UIApplication*)application performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem completionHandler:(void (^)(BOOL succeeded))completionHandler NS_AVAILABLE_IOS(9_0) { [_lifeCycleDelegate application:applicationperformActionForShortcutItem:shortcutItem completionHandler:completionHandler]; }- (void)application:(UIApplication*)application handleEventsForBackgroundURLSession:(nonnull NSString*)identifier completionHandler:(nonnull void (^)(void))completionHandler { [_lifeCycleDelegate application:application handleEventsForBackgroundURLSession:identifier completionHandler:completionHandler]; }- (void)application:(UIApplication*)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { [_lifeCycleDelegate application:application performFetchWithCompletionHandler:completionHandler]; }- (void)addApplicationLifeCycleDelegate:(NSObject<FlutterPlugin>*)delegate { [_lifeCycleDelegate addDelegate:delegate]; }
主要配置基本上已經(jīng)完成,只要在main.dart
實現(xiàn)Flutter的業(yè)務(wù)代碼即可
main.dart
實現(xiàn)的Flutter頁面,如: self.flutterViewController = [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil]; [self.navigationController pushViewController:self.flutterViewController animated:YES];
到這里現(xiàn)有iOS工程引入Flutter的工作就完成了,一些細(xì)節(jié)上的修改需要根據(jù)場景進(jìn)行修改,例如Flutter和Native的數(shù)據(jù)通信等。
日期:2018-10 瀏覽次數(shù):7353
日期:2018-12 瀏覽次數(shù):4421
日期:2018-07 瀏覽次數(shù):4954
日期:2018-12 瀏覽次數(shù):4253
日期:2018-09 瀏覽次數(shù):5592
日期:2018-12 瀏覽次數(shù):10011
日期:2018-11 瀏覽次數(shù):4895
日期:2018-07 瀏覽次數(shù):4662
日期:2018-05 瀏覽次數(shù):4945
日期:2018-12 瀏覽次數(shù):4398
日期:2018-10 瀏覽次數(shù):5222
日期:2018-12 瀏覽次數(shù):6296
日期:2018-11 瀏覽次數(shù):4552
日期:2018-08 瀏覽次數(shù):4674
日期:2018-11 瀏覽次數(shù):12735
日期:2018-09 瀏覽次數(shù):5661
日期:2018-12 瀏覽次數(shù):4922
日期:2018-10 瀏覽次數(shù):4264
日期:2018-11 瀏覽次數(shù):4613
日期:2018-12 瀏覽次數(shù):6146
日期:2018-06 瀏覽次數(shù):4088
日期:2018-08 瀏覽次數(shù):5534
日期:2018-10 瀏覽次數(shù):4534
日期:2018-12 瀏覽次數(shù):4619
日期:2018-07 瀏覽次數(shù):4446
日期:2018-12 瀏覽次數(shù):4593
日期:2018-06 瀏覽次數(shù):4478
日期:2018-11 瀏覽次數(shù):4455
日期:2018-12 瀏覽次數(shù):4335
日期:2018-12 瀏覽次數(shù):5358
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.