iOS原生项目(xcode15)嵌入Flutter模块

iOS原生项目接入Flutter作为模块使用

1.创建flutter模块到iOS项目中

flutter create --template module my_flutter

2.安装flutter依赖

flutter pub get

3.podFile文件配置

# Flutter模块路径
flutter_application_path = './my_flutter' # 修改为Flutter模块的实际路径
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target 'HelloFlutter' do
 
  use_frameworks! # 如果项目中需要使用动态库
  use_modular_headers! # 如果需要兼容部分库

  # 引入Flutter模块
  install_all_flutter_pods(flutter_application_path)

end

post_install do |installer|
  flutter_post_install(installer)
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      if config.name.include?("iphonesimulator")
        # 针对模拟器排除 arm64 架构
        config.build_settings['EXCLUDED_ARCHS'] = 'arm64'
      end
    end
  end
end

4.podFile安装依赖并且关闭用户脚本沙盒

pod install

!!!Build Settings 搜索 sandbox !!!
User Script Sandboxing 设置 NO

5.AppDelegate配置

#import "AppDelegate.h"
#import <Flutter/Flutter.h>
#import <FlutterPluginRegistrant/GeneratedPluginRegistrant.h>

@interface AppDelegate ()

@property (strong, nonatomic) FlutterEngine *flutterEngine;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//    self.window.backgroundColor = [UIColor whiteColor];
//    self.window.rootViewController = [[UIViewController alloc]init];
//    [self.window makeKeyAndVisible];
    
    // 初始化 window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 初始化 Flutter 引擎
    self.flutterEngine = [[FlutterEngine alloc] initWithName:@"io.flutter" project:nil];
    // 启动 Flutter 引擎
    [self.flutterEngine run];
    // 初始化 FlutterViewController,传入 FlutterEngine
    FlutterViewController *flutterViewController = [[FlutterViewController alloc] initWithEngine:self.flutterEngine nibName:nil bundle:nil];
    // 将 FlutterViewController 设置为根视图控制器
    self.window.rootViewController = flutterViewController;
    // 注册 Flutter 插件
    [GeneratedPluginRegistrant registerWithRegistry:flutterViewController];
    // 确保 window 可见
    [self.window makeKeyAndVisible];
    return YES;
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容