【问题标题】:Correct way of removing SceneDelegate from iOS 11+ project - What code is necessary in application(_: didFinishLaunchingWithOptions)从 iOS 11+ 项目中删除 SceneDelegate 的正确方法 - 应用程序中需要哪些代码(_:didFinishLaunchingWithOptions)
【发布时间】:2021-06-17 13:53:31
【问题描述】:

在 Xcode 12 中创建新的 iOS 项目时,会自动添加 UISceneDelegate。由于我的应用程序应该在 iOS 11+ 上可用(不支持 UISceneDelegate),我不得不将其删除。

info.plistAppDelegate 中删除UISceneDelegate 当然没有问题。但是我想知道是否必须向application(_: didFinishLaunchingWithOptions) 添加任何代码在大多数教程中我发现这个方法只是留空,只需要添加var window: UIWindow?。其他来源表明必须添加一些设置代码。

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
        /* Manual Setup */
        // let window = UIWindow(frame: UIScreen.main.bounds)    
        // window.rootViewController = ViewController() // Your initial view controller.
        // window.makeKeyAndVisible()
        // self.window = window

        return true
    }
} 

在我的测试中,一切正常,无需任何额外的设置代码。 rootViewController 是从 Storyboard 自动加载的,一切正常。

这只是巧合吗,这是在后台发生的一些 Xcode 魔术(如果代码丢失,自动添加 rootVC),还是我的代码(没有设置)损坏并最终在某个非常糟糕的时刻失败

【问题讨论】:

    标签: ios xcode appdelegate uiscenedelegate


    【解决方案1】:

    你只需要确定

    1- UISceneDelegate 被删除

    2- 密钥 UIApplicationSceneManifest 已从 Info.plist 中删除

    3- 这些方法从AppDelegate中删除

    // MARK: UISceneSession Lifecycle
    
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
    
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
    

    4- 将 var window: UIWindow? 添加到AppDelegate

    5- 确保在Main.storyboard中选择了入口点vc

    之后什么都别担心,将didFinishLaunchingWithOptions留空,就好像Xcode 11没有发生任何变化一样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-09
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      相关资源
      最近更新 更多