iOS现有工程集成Flutter(二)

2020-12-04  本文已影响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文件

Xcode 中集成 frameworks(拖拽framework到Xcode工程)

此方式比较简单

1.生成framework

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

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

# flutter_library 下 执行
flutter build ios

#iOS 工程创建
command+shift+N
image.png
2.iOS工程配置

拖拽framework 到 项目


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"
@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];
    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界面,

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界面,重启也不行,如果想更新需要重新添加App.framework

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

参考地址

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

上一篇下一篇

猜你喜欢

热点阅读