首页 > 解决方案 > AppDelegate 到 SceneDelegate

问题描述

我有一个没有sceneDelegate 的旧项目。我的 AppDelegate didFinishLaunchingWithOptions 看起来像:

import Firebase
import customFramework 
    
class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
        
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            
            FirebaseApp.configure()
            
            // Create window
            window = UIWindow(frame: UIScreen.main.bounds)
            window?.rootViewController = ViewController()
            window?.makeKeyAndVisible()
            
            
            
            customFrameworkManager.shared.start()
            
            return true
        }
    .
    .
    .

我想转换这段代码,因此决定创建一个新项目,但现在有一个场景委托的实现。我尝试改变我的 func 场景(willConnectTo),它看起来像:

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
            // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
            // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
        
        
        if let windowScene = scene as? UIWindowScene {
            self.window = UIWindow(windowScene: windowScene)
            
            self.window!.makeKeyAndVisible()
            
         
                }
        customFrameworkManager.shared.start()
    }

但是 customFramework 没有在应用程序中启动。谁能建议我在这里做错了什么。任何帮助,将不胜感激。谢谢

标签: iosswift

解决方案


在你的 sceneDelegate 中我没有看到 rootViewController,试试这样:

guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
let controller = ViewController()
window.rootViewController = controller
self.window = window
window.makeKeyAndVisible()
customFrameworkManager.shared.start()

推荐阅读