Flutter iOS混合开发(基于Flutter v1.10.15)

2019年08月23日 官方更新了混合方案, 相比之前简单了很多.

前提条件

需要切换到master分支, 然后更新FlutterSDK(最低版本1.8.4).
更新:如果你不是基于FlutterBoost混合开发, 那么建议你使用1.10+.
更新:如果你使用了FlutterSDK1.9.1 Xcode 编译会报错报错: Permission denied

flutter/packages/flutter_tools/bin/xcode_backend.sh
// 144行
RunCommand find "${derived_dir}/engine/Flutter.framework" -type f -exec chmod a-w "{}" \;
// 替换为
RunCommand find "${derived_dir}/engine/Flutter.framework" -type f -iname '.h' -exec chmod a-w "{}" \;
  • flutter 命令
// 查看分支
flutter channel

// 切换到master
flutter channel master

// 更新FlutterSDK
flutter upgrade

iOS 混合Flutter

Create a Flutter module

Add your Flutter app to your Podfile

  • step 1 Podfile 文件添加如下:
    # ../ 相当于跳转上一级目录
    flutter_application_path = '../../flutter/flutter_module_appaat/'
    load File.join(flutter_application_path, '.ios', 'Flutter', 'podhelper.rb')
    # 参考 step 2
    install_all_flutter_pods(flutter_application_path)
    # 以我的文件层级为例:
    # appaat_hybrid
    #  - android
    #  - flutter
    #     - flutter_module_appaat
    #  - ios
    #     - appaat-ios
    #       - ......
    #       - Podfile
  • step 2 对于每个需要嵌入Flutter的Xcode目标:
  target 'MyApp' do
    install_all_flutter_pods(flutter_application_path)
  end
  target 'MyAppTests' do
    install_all_flutter_pods(flutter_application_path)
  end
  • step 3 Run pod install
pod install
  • step 4 关闭 Bitcode
image.png

更新: 引用的Flutter插件导致Xcode编译不通过

target 'MyApp' do
# ...
end
# 强制关闭Bitcode
post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['ENABLE_BITCODE'] = 'NO'
        end
    end
end
  • 到此结束, 可以编译项目了.

接入FlutterViewController

  • AppDelegate
import UIKit
import Flutter
import FlutterPluginRegistrant // Only if you have Flutter Plugins.

@UIApplicationMain
class AppDelegate: FlutterAppDelegate {
  var flutterEngine : FlutterEngine?;
  // Only if you have Flutter plugins.
  override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    self.flutterEngine = FlutterEngine(name: "io.flutter", project: nil);
    self.flutterEngine?.run(withEntrypoint: nil);
    GeneratedPluginRegistrant.register(with: self.flutterEngine);
    return super.application(application, didFinishLaunchingWithOptions: launchOptions);
  }

}
  • ViewController
/// 自动跳转Flutter配置的main入库
let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine?
if flutterEngine != nil {
  let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)
  self.present(flutterViewController, animated: false, completion: nil)
}
  • 其他跳转方案
    更多细节查看这里

你可能感兴趣的:(Flutter iOS混合开发(基于Flutter v1.10.15))