美文网首页
关于iOS 13 SceneDelegate 适配

关于iOS 13 SceneDelegate 适配

作者: 飛天江郎 | 来源:发表于2020-01-06 20:02 被阅读0次

在XCode11 之后,在我们新建工程的时候发现多了一个新增文件 SceneDelegate.swift ,然后我们去看下这个文件到底是干什么用的WWDC2019:Optimizing App Launch在iOS 13之前,APP的生命周期和UI生命周期都是交由AppDelegate来处理的,在iOS 13以前,处理生命周期是下面这样的。

AppDelegate.png
这种模式是基于只有一个进程时适用,因为iOS一直以来都是一个进程运行时只有一个用户界面。在iOS 13之后,其实是为了iPadOS的多窗口支持,它可以高效的把应用委托工作分成两部分。
SceneDelegate承接了部分工作
所以我们看到的是这样承接了业务:
    // 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.
    }

这里是通过了func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration
把内容连接到了SceneDelegate
如下:

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
    }

但是,在很多情况下我们并不需要直接使用iOS 13这么高的版本,并且开发的应用程序也没有兼容iPadOS(很多时候,iPadOS版本的还是需要另外做个版本来适配),所以这里提出了两个方案来进行适配

1、添加版本判别运行代码@available

在使用 @available(iOS 13.0, *)进行判断,如果系统没到达13.0的话就不启用该段代码。

   @available(iOS 13.0, *)
    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)
    }

    @available(iOS 13.0, *)
    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.
    }

不过,还需要在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
    }

至于SceneDeleagte上也需要改成只有13.0以后才启用

import UIKit

@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
    }

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

2、去掉SceneDelegate相关内容

这是基于不需要使用多窗口支持而提出的解决方案,首先,需要去掉info.plist 的Application Scene Manifest选项,然后对应的SceneDelegate删除掉。
然后把对应的代码内容也注释掉或者删掉对应的内容,然后写上对应的生命周期,如下:

@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.
        return true
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

//    // 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.
//    }


}

相关文章

  • UIWindow(III)

    iOS13 UIWindow 适配 AppDelegate.m 文件 SceneDelegate.m 文件   这...

  • 关于iOS 13 SceneDelegate 适配

    在XCode11 之后,在我们新建工程的时候发现多了一个新增文件 SceneDelegate.swift ,然后我...

  • iOS13适配 - SceneDelegate

    借鉴:iOS 13 SceneDelegate适配 用XCode 11 创建的工程 在Xcode 11 创建的工程...

  • iOS 13 SceneDelegate适配(Xcode 11,

    文章目录 最低适配的系统改到iOS13以下后 解决方案一:删除SceneDelegate,还原到iOS13以前的版...

  • iOS 13 SceneDelegate适配

    Xcode 11新建工程在Xcode 11 创建的工程,运行设备选择 iOS 13.0 以下的设备,运行应用时会出...

  • iOS 13 SceneDelegate适配

    Xcode 11新建工程 在Xcode 11 创建的工程,运行设备选择 iOS 13.0 以下的设备,运行应用时会...

  • iOS 13 SceneDelegate适配

    Xcode 11新建工程 在Xcode 11 创建的工程,运行设备选择 iOS 13.0 以下的设备,运行应用时会...

  • iOS 13 SceneDelegate适配

    Xcode 11新建工程 在Xcode 11 创建的工程,运行设备选择 iOS 13.0 以下的设备,运行应用时会...

  • xcode11创建新项目

    1,如果项目不需要适配iOS13以下的机型。xcode11之后,入口放在了SceneDelegate里面。直接使用...

  • iOS 关于iOS13那些事

    本文记录一些关于iOS 13的内容,欢迎指正和补充! 一、关于iOS13适配 1.关于一些私有属性的适配,iOS ...

网友评论

      本文标题:关于iOS 13 SceneDelegate 适配

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