环境配置
Xcode版本:develop for iOS and macOS (Xcode 12.2)
Flutter版本:Flutter (Channel unknown, 1.20.0, on Mac OS X 10.15.6 19G2021, locale
VS Code版本 (version 1.51.0)
说明:不同的版本Flutter 生成的工程模板不同,编译后的App.frameork目录也不同,因此进行集成时需要比对版本。
flutter在 v1.20.0 编译生成的App.frameork 已经自包含flutter_assets文件
CocoaPods 依赖 FlutterSDK私有库方式(服务器下载SDK)
此方式比较简单
1.生成framework
flutter工程:界面Flutter开发
iOS工程:已有iOS工程,被集成的项目,
指定文件同级目录下:
#flutter工程创建
flutter create -t module flutter_library
# flutter_library 下 执行
flutter build ios
#iOS 工程创建
command+shift+N
image.png
制作私有库将编译文件进行私有库打包
pod lib create xxx // 创建私有库
pod repo spec push xxx // 推送文件到仓库
编译产物工程
image.png
组件库工程目录
image.png
2.iOS工程配置
2.1 配置AppDelegate
AppDelegate.h
@import UIKit;
@import Flutter;
@interface AppDelegate.m : FlutterAppDelegate <UIApplicationDelegate>
@property (nonatomic,strong) FlutterEngine *flutterEngine;
@property (strong, nonatomic) UIWindow * window;
@end
AppDelegate.m
#import "AppDelegate.m.h"
#import <HLFlutterSDK_Debug/GeneratedPluginRegistrant.h>
@interface AppDelegate.m ()
@end
@implementation HLAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.flutterEngine = [[FlutterEngine alloc] initWithName:@"Flutter Demo"];
[self.flutterEngine run];
[GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end
2.2 配置点击入口文件
#import "ViewController.h"
#import <Flutter/Flutter.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
flutterViewController.navigationItem.title = @"Flutter Demo";
[self presentViewController:flutterViewController animated:YES completion:nil];
}
@end
2.3 配置工程Podfile
use_frameworks!
platform :ios, '9.0'
target 'HLFlutterSDK_Example' do
pod 'HLFlutterSDK-Debug', :path => '../'
end
然后执行pod install 生成对应目录
运行项目,点击事件即可加载flutter界面,
3.注意点
- Xcode无需再配置
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build- 注意Flutter.dart标题匹配
flutterViewController.navigationItem.title = @"Flutter Demo";- 运行错误,
Dart Error: Can't load Kernel binary: Invalid kernel binary format version.
对应目录下重新执行:flutter build ios --debug- 更新问题
这种配置无法更新flutter界面,重启也不行,如果想更新需要重新升级App.framework,重新升版本
iOS现有工程集成Flutter(一)
iOS现有工程集成Flutter(二)
参考地址
环境搭建:https://www.jianshu.com/p/969aa7e37827
官方文档https://flutter.cn/docs/development/add-to-app/ios/project-setup











网友评论