从AppDelegate到SceneDelegate

时间:2020-11-07 02:55:02

标签: ios swift

我有一个正在使用的旧项目,该项目没有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
        }
    .
    .
    .

我想转换这段代码,因此决定创建一个新的新项目,但是现在有了场景Delegate的实现。我尝试更改如下所示的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没有在应用程序上启动。任何人都可以建议我在这里做什么错。任何帮助,将不胜感激。 谢谢

1 个答案:

答案 0 :(得分:0)

在您的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()