首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从iOS 11+项目中删除SceneDelegate的正确方法-应用程序中需要哪些代码(_:didFinishLaunchingWithOptions)

从iOS 11+项目中删除SceneDelegate的正确方法-应用程序中需要哪些代码(_:didFinishLaunchingWithOptions)
EN

Stack Overflow用户
提问于 2021-06-17 21:53:31
回答 1查看 117关注 0票数 0

在Xcode12中创建新的iOS项目时,会自动添加一个UISceneDelegate。由于我的应用程序应该在iOS 11+ (不支持UISceneDelegate)上可用,所以我不得不删除它。

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

代码语言:javascript
复制
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是从故事板自动加载的,一切工作正常。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-17 22:10:53

你只需要确保

1-删除UISceneDelegate

2-从Info.plist中删除密钥

3-这些方法从AppDelegate中删除

代码语言:javascript
复制
// 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-向AppDelegate添加var window: UIWindow?

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

在此之后,不要担心任何事情,并保留didFinishLaunchingWithOptions为空,就像Xcode 11中没有发生任何更改一样

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68020796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档