以下步骤帮助我将 Flutter 应用程序与 SceneDelegate 一起使用
- 创建包含子类
UIResponder 并符合UIWindowSceneDelegate 的SceneDelegate.swift
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
//....
}
- 将
SceneManifest 添加到Info.plist,在其中我们将SceneDelegate 声明为场景的默认配置
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<true/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
<key>Storyboard Name</key>
<string>Main</string>
</dict>
</array>
</dict>
</dict>
- 更新
AppDelegate.swift以支持iOS 13以外的其他版本
override func application( _ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
guard #available(iOS 13.0, *) else {
GeneratedPluginRegistrant.register(with: self)
guard let controller = window?.rootViewController as? FlutterViewController else { return true }
//Confugure 'controller' as needed
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
return true
}
- 更新
SceneDelegate.swift以支持iOS 13+
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
window = UIWindow(windowScene: windowScene)
let flutterEngine = FlutterEngine(name: "SceneDelegateEngine")
flutterEngine.run()
GeneratedPluginRegistrant.register(with: flutterEngine)
let controller = FlutterViewController.init(engine: flutterEngine, nibName: nil, bundle: nil)
window?.rootViewController = controller
window?.makeKeyAndVisible()
}