ToB企服应用市场:ToB评测及商务社交产业平台

标题: Flutter与iOS原生混合开辟 iOS项目集成Flutter [打印本页]

作者: 缠丝猫    时间: 2024-6-8 09:39
标题: Flutter与iOS原生混合开辟 iOS项目集成Flutter
1.创建flutter module
进入iOS工程根目次的上一级,创建flutter module工程
  1. flutter create --template module fluttermodule
复制代码
2.进入iOS工程根目次,编辑podfile文件
  1. flutter_application_path = '../fluttermodule'
  2. load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
  3. target "iOSDemo" do
  4. use_frameworks!
  5. install_all_flutter_pods(flutter_application_path)
  6. end
  7. post_install do |installer|
  8.   flutter_post_install(installer) if defined?(flutter_post_install)
  9. end
复制代码
实行pod install
3.iOS调用Flutter
在AppDelegate注册FlutterEngine
  1. import UIKit
  2. import Flutter
  3. import FlutterPluginRegistrant
  4. @main
  5. class AppDelegate: UIResponder, UIApplicationDelegate {
  6.     lazy var flutterEngine = FlutterEngine(name: "my flutter engine")
  7.     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  8.         // Override point for customization after application launch.
  9.         GeneratedPluginRegistrant.register(with: self.flutterEngine);
  10.         flutterEngine.run()
  11.         return true
  12.     }
  13. }
复制代码
利用 FlutterEngine调用utterViewController
  1. import UIKit
  2. import Flutter
  3. class ViewController: UIViewController {
  4.   override func viewDidLoad() {
  5.     super.viewDidLoad()
  6.     // Make a button to call the showFlutter function when pressed.
  7.     let button = UIButton(type:UIButton.ButtonType.custom)
  8.     button.addTarget(self, action: #selector(showFlutter), for: .touchUpInside)
  9.     button.setTitle("Show Flutter!", for: UIControl.State.normal)
  10.     button.frame = CGRect(x: 80.0, y: 210.0, width: 160.0, height: 40.0)
  11.     button.backgroundColor = UIColor.blue
  12.     self.view.addSubview(button)
  13.   }
  14.   @objc func showFlutter() {
  15.     let flutterEngine = (UIApplication.shared.delegate as! AppDelegate).flutterEngine
  16.     let flutterViewController =
  17.         FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
  18.     present(flutterViewController, animated: true, completion: nil)
  19.   }
  20. }
复制代码
参考链接
https://www.jianshu.com/p/aac1f387f3c0
https://www.jianshu.com/p/c3e5e210a585

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4