Flutter集成到已有iOS工程的方法步骤
作者:一禅小和尚 时间:2023-07-31 22:53:53
前言
之前写过一篇介绍flutter集成到Android工程的文章,这次总结记录一下自己把flutter集成到iOS的流程,以及遇到的问题以及和解决方法供大家参考。
创建flutter_module
要在iOS工程中集成flutter,首先我们需要创建一个flutter_module,创建的方法有两种:
使用Android studio创建
使用Android studio创建在上一篇新版Flutter集成到已有Android项目 中有介绍,这里不再赘述。
使用flutter命令创建
在指定目录下执行如下命令
flutter create --template module flutter_module
修改Podfile文件
在Podfile文件中的依赖部分添加如下内容
flutter_application_path'/Users/liuxinye/Desktop/WorkPro/AppGroup/platforms/xagf_flutter'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
然后在要集成flutter的target中添加
install_all_flutter_pods(flutter_application_path)
最后为iOS工程执行 pod install 即完成了了flutter_module的集成
使用flutter
接下来我们介绍如何在iOS工程中启用flutter进行开发
初始化FlutterEngine
首先我们需要在AppDelegate中持有一个FlutterEngine,代码如下:
@import Flutter
@interface AppDelegate
@property (strong, nonatomic) FlutterEngine *flutterEngine;
@end
然后我们需要在适当的时机启动 FlutterEngine
+(FlutterEngine *)initFlutter:(NSString*)entry :(NSString*)dartFile{
FlutterEngine *flutterEngine =
((AppDelegate *)UIApplication.sharedApplication.delegate).flutterEngine;
if (flutterEngine) {
return flutterEngine;
}else{
flutterEngine = [[FlutterEngine alloc] initWithName:@"my flutter engine"];
if(dartFile&&entry){
//指定dartFile文件中entry方法启动flutter
[flutterEngine runWithEntrypoint:entry libraryURI:dartFile];
}else if (entry){
//指定main.dart中方法名为entry的方法启动
[flutterEngine runWithEntrypoint:entry];
}else{
//执行默认的main.dart文件中的main方法进行启动
[flutterEngine run];
}
return flutterEngine;
}
}
这里涉及了三个Flutter的启动方式,三种方式的区别在代码中给出了注释。
当然,我们也可以在启动flutter时初始化路由。
[[flutterEngine navigationChannel] invokeMethod:@"setInitialRoute"
arguments:@"/index"];
至此我们就完成了 FlutterEngine 的启动工作,接下来我们来创建用来显示Flutter界面的 ViewContoller 。
创建FlutterViewController
[GeneratedPluginRegistrant registerWithRegistry:flutterEngine];
FlutterViewController *flutterViewController =
[[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
以UIView的方式使用
如果想要以 UIView 的方式使用,只需要使用 UIView *myFlutterView=flutterViewController.view
即可。
遇到的问题
此次集成Flutter到iOS工程还是比较顺利的,我在集成过程中只遇到了一个问题,在完成以上集成工作后运行iOS工程,出现了找不到 lib/main.dart 的报错,我百度了很久无法解决,最后尝试关闭xcode,删除 Pods 文件夹和 Podfile.lock 文件重新执行 pod install 解决了此问题。
来源:https://juejin.im/post/5e7c7d146fb9a00999229c1f
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#用Topshelf创建Windows服务的步骤分享
![](https://img.aspxhome.com/file/2023/4/76414_0s.png)
HashMap的get()方法的NullPointerException问题
Android开发Compose框架使用开篇
![](https://img.aspxhome.com/file/2023/8/94988_0s.jpg)
java开发线上事故理解RocketMQ异步精髓
![](https://img.aspxhome.com/file/2023/1/57501_0s.jpg)
Java通俗易懂系列设计模式之建造者模式
Eclipse中安装反编译工具Fernflower的方法(Enhanced Class Decompiler)
![](https://img.aspxhome.com/file/2023/1/77361_0s.png)
spring boot日志管理配置
Java面向接口编程之命令模式实例详解
![](https://img.aspxhome.com/file/2023/4/132054_0s.png)
详解房卡麻将分析系列 "牌局回放" 之 播放处理
![](https://img.aspxhome.com/file/2023/9/137359_0s.jpg)
使用SpringBoot配置多数据源的经验分享
![](https://img.aspxhome.com/file/2023/1/97931_0s.png)
android实现raw文件夹导入数据库代码
c#中LINQ的基本用法(二)
![](https://img.aspxhome.com/file/2023/9/92969_0s.png)
Android6.0蓝牙出现无法扫描设备或闪退问题解决办法
![](https://img.aspxhome.com/file/2023/5/139605_0s.jpg)
介绍Jersey-Jersey入门基础
深入浅析Android坐标系统
![](https://img.aspxhome.com/file/2023/8/106818_0s.png)
java8 stream的多字段排序实现(踩坑)
![](https://img.aspxhome.com/file/2023/5/83085_0s.jpg)
Java 面试题和答案 - (下)
Maven分步详解多环境配置与应用流程
![](https://img.aspxhome.com/file/2023/9/122309_0s.png)
springboot解决java.lang.ArrayStoreException异常
![](https://img.aspxhome.com/file/2023/0/76250_0s.jpg)