iOS、SwiftFlutter

iOS原生项目使用Flutter module

2021-01-29  本文已影响0人  刘铁崧

iOS使用cocoapods集成Flutter module

1.podfile文件中引入对应flutter项目路径并install

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

flutter_application_path = '../flutter_module/'
load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')

target 'testFlutterModule' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
  install_all_flutter_pods(flutter_application_path)
  # Pods for testFlutterModule
end

2.Appdelegate中创建Flutter引擎并运行

import UIKit
import Flutter
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    // 创建Flutter引擎
    lazy var flutterEngine : FlutterEngine = FlutterEngine(name:"cy.com")
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        //启动Flutter引擎
        flutterEngine.run()
        return true
    }
}

3.Controller中引入Flutter页面

import UIKit
import Flutter
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 创建FlutterViewController
        let button = UIButton(type: .custom)
        button.addTarget(self, action: #selector(showFlutterController), for: .touchUpInside)
        button.frame = CGRect(x: 20, y: 200, width: 200, height: 50)
        button.backgroundColor = UIColor.blue
        self.view.addSubview(button)  
    }
    @objc func showFlutterController(){
        // 获取flutter引擎
        let engine = (UIApplication.shared.delegate as!AppDelegate).flutterEngine
        //创建FlutterViewController
        let flutterVC = FlutterViewController(engine: engine, nibName: nil, bundle: nil)
        flutterVC.modalPresentationStyle = .fullScreen//设置弹出效果
        present(flutterVC, animated: true, completion: nil) 
    }
}

注:如果想要flutter模块支持热更新可以在flutter项目中使用如下指令:

flutter attach -d 设备 --app-id 对应APPID

需要热更新时:r —— reload / R —— restart

上一篇 下一篇

猜你喜欢

热点阅读