IOS开发

iOS13新特性 - SceneDelegate

2020-01-10  本文已影响0人  沉江小鱼

SceneDelegate是为了支持 iOS13之后的 iPadOS 多窗口口而退出的。Xcode11 默认会创建通过 UIScene 管理多个 UIWindow 的应用,工程中除了 AppDelegate 外会多一个 SceneDelegate。并且 AppDelegate.h 不再有 window 属性,window 属性被定义在了 SceneDelegate.h 中,SceneDelegate 负责原 AppDelegate 的 UI 生命周期部分的职责。

这种模式完全没有问题,因为只有一个进程,只有一个与这个进程对应的用户界面。

image.png image.png
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    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 {
        
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

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

    }
}

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

    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }
}

对于这个特性的适配

如果使用纯代码来实现显示界面,需要在 AppDelegate中手动添加 window 属性:

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

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow(frame: UIScreen.main.bounds);
        window?.backgroundColor = .white
        window?.rootViewController = ViewController()
        window?.makeKeyAndVisible()
        
        return true
    }
}
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 }
        
        self.window = UIWindow(windowScene: scene as! UIWindowScene)
        self.window?.frame = UIScreen.main.bounds
        self.window?.backgroundColor = .white
        self.window?.rootViewController = ViewController1()
        self.window?.makeKeyAndVisible()
    }

下面是关于UIScene 以及 SceneDelegate 的一些介绍了。

UIScene 的介绍

表示应用程序用户界面实例的对象。

描述:

方法列表
** 通过制定的UISceneSession对象和UIScene.ConnectionOptions创建一个 UIScene**
** UISceneSession包含了一些配置信息**
public init(session: UISceneSession, connectionOptions: UIScene.ConnectionOptions)
** 实现UISceneDelegate方法的代理对象**
open var delegate: UISceneDelegate?
** Scene的当前执行状态。
** @available(iOS 13.0, *)
    public enum ActivationState : Int {
        case unattached  未连接到应用程序的状态。
        case foregroundActive 在前台运行
        case foregroundInactive 在前台运行正在接收事件
        case background 后台运行
    }
open var activationState: UIScene.ActivationState { get }

** Scene 的标题,系统会在应用切换器中显示这个字符串
open var title: String!
** 使用这个属性告诉UIKIt什么时候你想激活这个场景。
open var activationConditions: UISceneActivationConditions
** 只读属性,UIKit为每个场景维护一个会话对象。session对象包含场景的唯一标识符和关于其配置的其他信息。
open var session: UISceneSession { get }
** 异步地加载指定的 URL,使用此方法打开指定的资源。如果指定的URL模式由另一个应用程序处理,iOS将启动该应用程序并将URL传递给它。启动应用程序将另一个应用程序带到前台。
open func open(_ url: URL, options: UIScene.OpenExternalURLOptions?, completionHandler completion: ((Bool) -> Void)? = nil)
    @available(iOS 13.0, *)   UIKit向应用添加了一个场景
    public class let willConnectNotification: NSNotification.Name

    @available(iOS 13.0, *)  UIKit 从应用中移除了一个场景
    public class let didDisconnectNotification: NSNotification.Name

    @available(iOS 13.0, *)  指示场景现在在屏幕上,并相应用户事件
    public class let didActivateNotification: NSNotification.Name

    @available(iOS 13.0, *)  指示场景将退出活动状态并停止相应用户事件
    public class let willDeactivateNotification: NSNotification.Name

    @available(iOS 13.0, *)  场景即将开始在前台运行
    public class let willEnterForegroundNotification: NSNotification.Name

    @available(iOS 13.0, *)  场景在后台运行
    public class let didEnterBackgroundNotification: NSNotification.Name

UISceneConnectionOptions

连接选项
描述:
UIKit 创建scene 有很多原因,它可以响应切换请求或打开 URL 的请求,当有创建场景的特定原因时,UIKit 用关联的数据填充 UISceneConnectionOptions 对象,并在连接时将其传递给代理,使用此对象中的信息进行相应的响应。例如,如果是打开 UIKit 提供的 url,我们可以在场景中显示他们的内容。
不要直接创建UISceneConnectionOptions对象,UIKit 会自动创建,并将其传递给场景代理的scene:willConnectToSession:options:方法。

** 要打开的url,以及指定如何打开它们的元数据。
open var urlContexts: Set<UIOpenURLContext> { get }

** 发起请求的应用程序的boundleID。
open var sourceApplication: String? { get }

** 挂起的切换活动的类型。
open var handoffUserActivityType: String? { get }

** 恢复场景的先前状态,用于将应用还原到以前状态的用户活动信息。
open var userActivities: Set<NSUserActivity> { get }

** 用户对应用程序通知之一的响应。        
open var notificationResponse: UNNotificationResponse? { get }

** 处理快速动作,用户选择要执行的操作
open var shortcutItem: UIApplicationShortcutItem? { get }

** 有关应用程序现在可用的CloudKit数据的信息。
open var cloudKitShareMetadata: CKShareMetadata? { get }
上一篇 下一篇

猜你喜欢

热点阅读