Flutter与iOS原生混合开辟 iOS项目集成Flutter

打印 上一主题 下一主题

主题 656|帖子 656|积分 1968

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企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

缠丝猫

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表