美文网首页
iOS现有工程集成Flutter(一)

iOS现有工程集成Flutter(一)

作者: 一抹相思泪成雨 | 来源:发表于2020-12-04 22:59 被阅读0次

环境配置

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 和 Flutter SDK 集成(相对目录)

Flutter.framework 是 Flutter engine 的框架
App.framework 是 Dart 代码的编译产物

1.工程创建

flutter工程:界面Flutter开发
iOS工程:已有iOS工程,被集成的项目,

指定文件同级目录下:
#flutter工程创建
flutter create -t module flutter_library

# flutter_library 下 执行
flutter build ios

#iOS 工程创建
command+shift+N
image.png
2.iOS工程配置 Podfile
platform :ios, '9.0'
flutter_application_path = '../flutter_library/'   #相对目录
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
target 'Flutter_iOS' do
  # Pods for Flutter_iOS
  install_all_flutter_pods(flutter_application_path)
end

然后执行pod install 生成对应目录

image.png

2.1 配置AppDelegate

AppDelegate.h
#import <UIKit/UIKit.h>
#import <Flutter/Flutter.h>
@interface AppDelegate : FlutterAppDelegate <UIApplicationDelegate>
@property (nonatomic,strong) FlutterEngine *flutterEngine;
@end

AppDelegate.m
#import "AppDelegate.h"
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h> // 如果你需要用到Flutter插件时
@interface AppDelegate ()
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.flutterEngine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];
    [self.flutterEngine runWithEntrypoint:nil];
    [GeneratedPluginRegistrant registerWithRegistry:self.flutterEngine]; //如果你需要用到Flutter插件时
    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

运行项目,点击事件即可加载flutter界面,对应lib/main.dart 文件

3.注意点
  1. Xcode无需再配置
    "$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
  2. 注意Flutter.dart标题匹配
    flutterViewController.navigationItem.title = @"Flutter Demo";
  3. 运行错误,
    Dart Error: Can't load Kernel binary: Invalid kernel binary format version.
    对应目录下重新执行: flutter build ios --debug
  4. 更新问题
    这种配置可更新flutter界面,单需要重启或pod intsall

iOS现有工程集成Flutter(二)
iOS现有工程集成Flutter(三)

参考地址

环境搭建:https://www.jianshu.com/p/10237bf13789
官方文档https://flutter.cn/docs/development/add-to-app/ios/project-setup

相关文章

网友评论

      本文标题:iOS现有工程集成Flutter(一)

      本文链接:https://www.haomeiwen.com/subject/eirwwktx.html