Xcode11新建项目 & iOS13 适配SceneDelegate

我们把Xcode升级到11.0以后,新建工程的时候就会多一个SceneDelegate 文件,这是苹果兼容iPadOS新添加的文件。

1、 Xcode 11 默认是会创建通过 UIScene 管理多个 UIWindow 的应用,工程中除了 AppDelegate 外会多一个 SceneDelegate
2、AppDelegate和SceneDelegate这是iPadOS带来的新的多窗口支持的结果,并且有效地将应用程序委托的工作分成两部分。

那AppDelegate 和 SceneDelegate到底有什么区别
  • 在iOS 13以后AppDelegate的功能发生了变化,把一部分功能交给了SceneDelegate处理
    在iOS13 以前AppDelegate的功能,全权处理App生命周期和UI生命周期,如下图。
iOS13 以前AppDelegate功能图
  • 在iOS 13以后AppDelegate的功能 和 SceneDelegate功能对照图
iOS 13以后AppDelegate功能图

SceneDelegate功能图
SceneDelegate适配
方案一

如果我们不开发iPadOS多窗口APP,SceneDelegate窗口管理我们可以不需要直接删掉就好了。
1.删除掉info.plist中Application Scene Manifest选项,同时,文件SceneDelegate可删除可不删
2.相关代码注释掉

//注释下面两个方法
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    }
//在AppDelegate 中添加window 属性
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow.init()
        window?.frame = UIScreen.main.bounds
        window?.makeKeyAndVisible()
        window?.rootViewController = UIViewController.init()
        return true
        
    }
方案二

即要用iOS 13中新的SceneDelegate,又可以在iOS 13一下的设备中完美运行。那就添加版本判断,利用@available

1.SceneDelegate中添加@available(iOS 13, *)

import UIKit
@available(iOS 13, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        let windowScene:UIWindowScene = scene as! UIWindowScene
        window = UIWindow.init(windowScene: windowScene)
        window?.backgroundColor = UIColor.green
        window?.frame = UIScreen.main.bounds
        window?.makeKeyAndVisible()
        let tt = UIViewController.init()
        tt.view.backgroundColor = UIColor.brown
        window?.rootViewController = tt
        guard let _ = (scene as? UIWindowScene) else { return }
    }
}

2.AppDelegate中同样声明window属性,AppDelegate中两个关于Scene的类也添加版本控制,Swift中可以用扩展单独拎出来,如下:

@UIApplicationMain

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

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        if #available(iOS 13, *) {
        }else {
            window = UIWindow.init()
            window?.frame = UIScreen.main.bounds
            window?.makeKeyAndVisible()
            window?.rootViewController = UIViewController.init()
        }
        return true
        
    }

   

}

@available(iOS 13.0, *)
extension AppDelegate {
    // MARK: UISceneSession Lifecycle
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    }
}

本文参考1:https://blog.csdn.net/weixin_38735568/article/details/101266408
本文参考2:https://www.jianshu.com/p/801de3beee22

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 抖音极速版:新用户秒提1.3元,每分享一位奖励38元现金 ! 猫爱上鸡腿 简书作者 2019-09-25 19:2...
    刘家大院_973c阅读 175评论 0 0
  • 自从毕业之后,还没有褪去的孩子气和满身的刺,带着满心的期待和自身满满的动力对第一份实习工作充满了动力,没想到经历了...
    甜心啵阅读 111评论 0 1
  • 这个夏天,我毕业了。 领毕业证的那天下了很大很大的雨,站在校门口的长亭里,看着来来去去的人群,突然间仿佛看到昨天进...
    柠檬小新dy阅读 188评论 1 0
  • 2019年3月4日 星期一 晴 今天的天气看起来,还是那么的忧郁,不过回来这么久,早已经习惯了这种天气,并且开始...
    文道的杂货铺阅读 610评论 5 19
  • 002/180 我是微蓝财富俱乐部负责人九胖。 剽悍江湖11期优秀学员。 剽悍江湖9.0运营学院成员。 今天来晚了...
    猪九胖阅读 211评论 0 2