前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 项目集成Flutter

iOS 项目集成Flutter

作者头像
大话swift
发布2019-07-03 16:27:05
1.4K0
发布2019-07-03 16:27:05
举报
文章被收录于专栏:大话swift大话swift

最近移动端火爆无非是Flutter--舍我其谁,而官网的引导之中鲜有说怎么在已有项目中去集成Flutter,今天我们就再蹭个热门来进行一个iOS集成

1 创建iOS项目(做测试使用)

2 cocoapods走起

project 'XX.xcodeproj' #source 'https://github.com/CocoaPods/Specs.git' #platform :ios, '9.0' #inhibit_all_warnings! # Uncomment the next line to define a global platform for your project # platform :ios, '9.0'target 'XX' do # Uncomment the next line if you're using Swift or would like to use dynamic frameworks # use_frameworks! # Pods for XX target 'XXTests' do inherit! :search_paths # Pods for testing end target 'XXUITests' do inherit! :search_paths # Pods for testing endend flutter_application_path = './flutter_module' eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)

关键部锋分就是末尾添加的

代码语言:javascript
复制
flutter_application_path = './flutter_module'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)

3 进入项目根目录添加flutter_module

flutter create -t module flutter_module

4 Xcode 设置 Build Phases 添加 Run Scripts

"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build

4 pod install

5 code设置 5.1 Appdelegate

#import <Flutter/Flutter.h> @interface AppDelegate : FlutterAppDelegate //@property (strong, nonatomic) UIWindow *window; @end

#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h> // Only if you have Flutter Plugins- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [GeneratedPluginRegistrant registerWithRegistry:self]; return [super application:application didFinishLaunchingWithOptions:launchOptions]; }

5.2 Controller切换

- (IBAction)pushFlutterVC:(id)sender { FlutterViewController *flutterViewController = [[FlutterViewController alloc] init]; flutterViewController.view.backgroundColor = [UIColor cyanColor]; [flutterViewController setInitialRoute:@"route1"]; [self presentViewController:flutterViewController animated:YES completion:^{ UIViewController * vc = [[NextViewController alloc] init]; // vc.view.backgroundColor = [UIColor whiteColor]; AppDelegate *appDelegate = [UIApplication sharedApplication].delegate; UIViewController * rootVC = appDelegate.window.rootViewController; UIViewController * topVC = nil; if ([rootVC isKindOfClass:[UINavigationController class]]) { topVC = ((UINavigationController *)rootVC).topViewController.presentedViewController; } else { topVC = rootVC.presentedViewController; } [topVC presentViewController:vc animated:YES completion:nil]; }]; }

5.2中我们要考虑到首页是Nav UIViewCOntrooler 或者是FlutterViewController 到此我们的就可以直接Xcode run运行项目啦

6 假如想实现hot reload

进入项目中的flutter_module,运行 flutter attach

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-12-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大话swift 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档