iOS(swift)老项目中加入Flutter

1. 新建Flutter项目或者从代码仓库pull一个项目到本地,命令如下:

flutter create -t module flutter_module

2. 新建Config目录,在这个目录下新建3个配置文件

  • Flutter.xccofig,内容如下:
#include "../../flutter_module/.ios/Flutter/Generated.xcconfig"
ENABLE_BITCODE=NO

其中”flutter_module“为Flutter项目目录,我的Flutter和iOS项目差了2个目录,所以在前面有2个../../,不把2个放在同级别,是因为Flutter项目是有自己的git,让2个项目的git分开


iOS(swift)老项目中加入Flutter_第1张图片
项目目录结构.png
  • Debug.xccofig,内容如下:
#include "Flutter.xcconfig"
#include "Pods/Target Support Files/Pods-iOS项目名称/Pods-iOS项目名称.debug.xcconfig"

如果没有pod管理,第二行可以不用。其中”iOS项目名称“改成自己的项目名称即可

  • Release.xccofig,内容如下:
#include "Flutter.xcconfig"
#include "Pods/Target Support Files/Pods-iOS项目名称/Pods-iOS项目名称.debug.xcconfig"
FLUTTER_BUILD_MODE=release

3. 修改Xcode配置

  • 关闭Enable Bitcode


    iOS(swift)老项目中加入Flutter_第2张图片
    image.png
  • 修改debug(我没改也能运行,不知道为啥)


    iOS(swift)老项目中加入Flutter_第3张图片
    image.png
  • 添加Run Script
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" build
"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh" embed
iOS(swift)老项目中加入Flutter_第4张图片
image.png

4. 修改pod,增加如下内容

flutter_application_path = '../../flutter_module'
eval(File.read(File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')), binding)

然后执行pod install

5.修改APPDelegate.swfit

import UIKit
import SVProgressHUD
import FlutterPluginRegistrant

@UIApplicationMain
class AppDelegate: FlutterAppDelegate {
//    var window: UIWindow?
    override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let loginVC = LoginViewController()
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = loginVC
        self.window?.makeKeyAndVisible()
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}

6. 在项目中运行代码即可(ios编译运行前,先运行编译flutter项目,生成一些必要的文件)

import Flutter

let vc = FlutterViewController()
self.present(vc, animated: true, completion: nil)

参考文章:
iOS老项目集成Flutter(iOS混编Flutter)
添加Flutter到现有iOS的项目
iOS Native混编Flutter交互实践

你可能感兴趣的:(iOS(swift)老项目中加入Flutter)