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生命周期,如下图。
738839-595e8a80104972f8.png
iOS13 以前AppDelegate功能图
  • 在iOS 13以后AppDelegate的功能 和 SceneDelegate功能对照图
738839-0d9367533f3ced83.png
iOS 13以后AppDelegate功能图

738839-495e5d5aedba2b0f.png
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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值