【问题标题】:How to have a flutter method handler in SceneDelegate?如何在 SceneDelegate 中有一个颤振方法处理程序?
【发布时间】:2020-12-17 05:50:32
【问题描述】:

我有一个音乐颤振应用程序,现在我正在 swift 中实现本机播放器代码,该应用程序与 spotify ios sdk 集成。

我正在尝试运行,但 IOS 13.6 手机上无法使用 spotify 连接功能,我看到 IOS 13+ 更高版本需要 SceneDelegate。

现在我被困在如何在 SceneDelegate 中添加 Flutter 方法处理程序。

当我在 SceneDelegate 上添加 Flutter 方法处理程序时,我收到以下错误,它将卡在断点处,下一个不会执行。

我正在尝试按照 AVFoundation 文档制作一个简单的视频取景器。应用程序每次启动时都会终止。如何解决此特定错误?

Thread 1: EXC_BREAKPOINT (code=1, subcode=0x102d3c320)

【问题讨论】:

  • flutter 不支持 SceneDelegate 并且将它与 Flutter 一起使用是错误的方法

标签: ios swift flutter


【解决方案1】:

以下步骤帮助我将 Flutter 应用程序与 SceneDelegate 一起使用

  1. 创建包含子类UIResponder 并符合UIWindowSceneDelegateSceneDelegate.swift
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?
    //....

}
  1. 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>
  1. 更新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
    }
  1. 更新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()
    }

【讨论】:

  • 抱歉,很晚才回复此主题。实际上后来我发现场景代表并不是全部必需的。 Spotify SDK 与 App Delegate 配合得很好。感谢您的回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
  • 2020-08-20
  • 2020-12-17
  • 2020-04-13
相关资源
最近更新 更多