测验在原iOS工程里增加Flutter模块,碰到问题,发现网上的教程比较陈腐,所以写一篇最新教程。 ##创立Flutter moudle 一般把Flutter moudle放在iOS工程同级,然后履行指令

cd 项目根目录
flutter create --template module my_flutter

履行完毕后,Flutter module将会创立在 ios项目/my_flutter目录下,目录结构如下图:

添加Flutter到iOS工程最新教程
.ios 是隐藏目录,能够独自运转Flutter module,测试此模块的功能,iOS代码增加到现有应用程序的项目或插件中,而不是增加到模块的.ios /目录中。因为.ios /目录是主动生成的,因此请勿对其进行源代码操控。在新机器上构建模块之前,请先在my_flutter目录中运转flutter pub get来从头生成.ios /目录,然后再运用Flutter模块构建iOS项目。

运用CocoaPods将Flutter模块嵌入到现有应用程序中

修正iOS应用程序中 Podfile 文件,内容如下:

添加Flutter到iOS工程最新教程
需求注意的是,Flutter模块和iOS工程位于同级目录中,如果在不同的目录中,需求调整相对路径。 履行pod install指令,履行完毕后,再通过指令生成Frameworks:

cd /Users/xxx/Desktop/iOS\ Flutter/my_flutter
flutter build ios-framework --output=./Flutter/

添加Flutter到iOS工程最新教程
Frameworks 现已生成,然后将 App.framework 和 Flutter.framework 拖入Build Settings > Build Phases > Link Binary With Libraries:
添加Flutter到iOS工程最新教程
此刻在项目的左侧增加 Frameworks 目录:

添加Flutter到iOS工程最新教程
然后开端运转,发现报错:

添加Flutter到iOS工程最新教程
解决办法: 找到Build Phases > [CP-User],直接叉掉即可。

创立 FlutterEngine 和 FlutterViewController

将 Flutter 页面嵌入 iOS 应用程序需求创立 FlutterEngine(Flutter 引擎) 和 FlutterViewController,FlutterEngine 是Dart VM和Flutter运转时的 host,FlutterViewController 附着于 FlutterEngine,作用是通信和显现 Flutter UI。

创立 FlutterEngine:

import UIKit
import Flutter
@UIApplicationMain
class AppDelegate: FlutterAppDelegate {
  lazy var flutterEngine = FlutterEngine(name: "my flutter engine")
  override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    flutterEngine.run();
    return super.application(application, didFinishLaunchingWithOptions: launchOptions);
  }
}

增加一个按钮,跳转到 Flutter 页面:


import UIKit
import Flutter
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let button = UIButton(type:UIButton.ButtonType.custom)
        button.addTarget(self, action: #selector(showFlutter), for: .touchUpInside)
        button.setTitle("显现 Flutter", for: UIControl.State.normal)
        button.frame = CGRect(x: 80.0, y: 210.0, width: 160.0, height: 40.0)
        button.backgroundColor = UIColor.blue
        self.view.addSubview(button)
    }
    @objc func showFlutter() {
      let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
      let flutterViewController =
          FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
      present(flutterViewController, animated: true, completion: nil)
    }
}

添加Flutter到iOS工程最新教程

上面的代码运用了缓存 FlutterEngine,当然这也是推荐的一种办法。还有一种办法是是运用隐含的FlutterEngine,运用隐含的FlutterEngine会明显增加显现Flutter UI的时刻,一般不主张这样做,如果很少显现 Flutter 屏幕,没有好的办法来确认何时启动Dart VM以及何时Flutter不需求在视图操控器之间坚持状况,则这或许很有用。

func showFlutter() {
  let flutterViewController = FlutterViewController(project: nil, nibName: nil, bundle: nil)
  present(flutterViewController, animated: true, completion: nil)
}

指定进口点

默许情况下 FlutterEngine 加载 lib/main.dart 文件中的 main() 办法,也能够指定其他文件的办法:

flutterEngine.run(withEntrypoint: "newEntrypoint", libraryURI: "main.dart")

初始化路由

从Flutter 1.22版开端,能够指定路由

let flutterEngine = FlutterEngine()
flutterEngine.run(
  withEntrypoint: FlutterDefaultDartEntrypoint, initialRoute: "/one_page")