我正在努力将 Flutter 模块集成到 Swift 应用程序中。我遵循了 Flutter 文档(将 Flutter 模块集成到您的 iOS 项目中),并尝试了所有三个选项,但不幸的是只有选项 A(嵌入 CocoaPods 和 Flutter SDK)有效,这是我唯一不能的选项使用,因为它会暴露我的源代码(我不能)。目前我已经尝试最小化代码,但仍然无法正常工作。
2021-04-15 19:12:55.694052-0300 libertyModuleIOS[78482:2330559] Failed to find snapshot: /Users/user/Library/Developer/CoreSimulator/Devices/34DB7C2C-0AF9-4F97-B94A-125550BB4806/data/Containers/Bundle/Application/8623C44A-A239-44C7-8474-5765E77BD28B/libertyModuleIOS.app/Frameworks/App.framework/flutter_assets/kernel_blob.bin
2021-04-15 19:12:55.694815-0300 libertyModuleIOS[78482:2330559] Metal API Validation Enabled
2021-04-15 19:12:55.772496-0300 libertyModuleIOS[78482:2331070] [VERBOSE-2:engine.cc(182)] Engine run configuration was invalid.
2021-04-15 19:12:55.772980-0300 libertyModuleIOS[78482:2331070] [VERBOSE-2:shell.cc(571)] Could not launch engine with configuration.
2021-04-15 19:12:56.148194-0300 libertyModuleIOS[78482:2331083] flutter: Observatory listening on http://127.0.0.1:57253/9vJInMBBG9w=/
有趣的是,在我的物理设备(Iphone SE)上它运行良好,但是当我将它上传到 TestFlight 或尝试在模拟器上运行它(甚至在 iPhone SE 上)时,它不起作用,并显示上面的错误(当然只在模拟器上)。顺便说一句,我不使用 CocoaPods 来生成代码(我已经尝试将它与第三个选项一起使用,但是“出来”了同样的问题)。
我使用的 Swift 代码与 Flutter 文档中的完全相同。
AppDelegate.swift
import UIKit
import Flutter
// Used to connect plugins (only if you have plugins with iOS platform code).
import FlutterPluginRegistrant
@UIApplicationMain
class AppDelegate: FlutterAppDelegate { // More on the FlutterAppDelegate.
lazy var flutterEngine = FlutterEngine(name: "my flutter engine")
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Runs the default Dart entrypoint with a default Flutter route.
flutterEngine.run();
// Used to connect plugins (only if you have plugins with iOS platform code).
GeneratedPluginRegistrant.register(with: self.flutterEngine);
return super.application(application, didFinishLaunchingWithOptions: launchOptions);
}
}
ViewController.swift
import UIKit
import Flutter
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Make a button to call the showFlutter function when pressed.
let button = UIButton(type:UIButton.ButtonType.custom)
button.addTarget(self, action: #selector(showFlutter), for: .touchUpInside)
button.setTitle("Show 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)
}
}