美文网首页
iOS 移除 SceneDelegate

iOS 移除 SceneDelegate

作者: 一滴矿泉水 | 来源:发表于2022-01-29 14:02 被阅读0次

    xcode11创建项目新增SceneDelegate文件,AppDelegate文件结构也发生变化,在AppDelegate.h文件中没有了window属性,而是在SceneDelegate.h中,可见AppDelegate不管理window而是交给SceneDelegate。由于这些是ios13新增,所以SceneDelegate在ios13以下的系统是不支持。

    那么如果我的App不需要支持多个Scene ,或者需要兼容 iOS13.0以下的版本怎么办呢,其实很简单,移除掉 SceneDelegate 即可。

    1、工程中删除 SceneDelegate 文件
    2、和往常一样在 AppDelegate 中添加 window 属性
    class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }
    
    3、移除 AppDelegate 中多余的 UIScene 代理方法
    移除前
    import UIKit
    
    @main
    class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }
    
    // 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.
    }
    

    }

    移除后
    import UIKit
    
    @main
    class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
    
        return true
    }
    

    }

    4、最后在info.plist文件中移除Application Scene Manifest
    截屏2022-01-29 下午12.00.06.png

    文章持续更新中、希望对各位有所帮助、有问题可留言 大家共同学习.

    相关文章

      网友评论

          本文标题:iOS 移除 SceneDelegate

          本文链接:https://www.haomeiwen.com/subject/zdzxkrtx.html