iOS13以后,SceneDelegate将负责AppDelegate的某些功能。 window(窗口)的概念被window(场景)的概念所代替, 一个scene现在可以作为您应用程序的用户界面和内容的载体。iOS13以前一个应用程序可以有不止一个window,同样现在一个应用程序也可以有不止一个scene。
iOS13以后,Xcode新建iOS项目中有增加三处新增内容:
新增SceneDelegateClass
scene sessions
相关的新方法:application(_:configurationForConnecting:options:)
application(_:didDiscardSceneSessions:)
Application Scene Manifest
配置项,用于配置App的scene,包括它们的scene配置名,delegate类名和storyboard下面分别讲解下新增三处内容:
SceneDelegate和AppDelegate中方法名相似, 是任何应用程序生命周期都会调用方法。
//SceneDelegate.swift 代码
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene) { }
func sceneDidBecomeActive(_ scene: UIScene) { }
func sceneWillResignActive(_ scene: UIScene) { }
func sceneWillEnterForeground(_ scene: UIScene) { }
func sceneDidEnterBackground(_ scene: UIScene) { }
}
scene(_:willConnectTo:options:)
函数 : SceneDelegate的最重要的函数,相当于iOS 12上的 application(_:didFinishLaunchingWithOptions:)
函数。当将scene添加到app中时scene(_:willConnectTo:options:)
函数会被调用的,因此在这里对scene进行配置。 这里需要特别注意的是,使用一个SceneDelegate来配置App中的所有scene,并且这个delegate通常会响应任何scene。在上面的代码中,我们可以手动地设置了视图控制器堆栈,稍后会进行详细介绍。SceneDelegate其他方法:
sceneDidDisconnect(_:)
当scene与app断开连接是调用(注意,以后它可能被重新连接)sceneDidBecomeActive(_:)
当用户开始与scene进行交互(例如从应用切换器中选择场景)时,会调用sceneWillResignActive(_:)
当用户停止与scene交互(例如通过切换器切换到另一个场景)时调用sceneWillEnterForeground(_:)
当scene变成活动窗口时调用,即从后台状态变成开始或恢复状态sceneDidEnterBackground(_:)
当scene进入后台时调用,即该应用已最小化但仍存活在后台中//AppDelegate.swift 代码
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
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>) {
}
}
在iOS13中AppDelegate中新增的两个函数是负责管理Senen Session的代理函数。在应用创建scene(场景)后,scene session
对象将跟踪与该scene相关的所有信息。
这两个函数是:
application(_:configurationForConnecting:options:)
: 方法会返回一个UISceneConfiguration对象,其中包含场景详细信息,包括要创建的场景类型,用于管理场景的代理对象以及包含要显示的初始视图控制器的StoryBoard。 如果未实现此方法,则必须在应用程序的Info.plist文件中提供场景配置数据。
注意:该代理方法中返回UISceneConfiguration对象的配置名为Default Configuration,则系统就会自动去调用SceneDelegate这个类。这样SceneDelegate和AppDelegate产生了关联。application(_:didDiscardSceneSessions:)
: 在分屏中关闭其中一个或多个scene时候回调用,可以在该函数中销毁场景所使用的资源。
该方法与application(_:didDiscardSceneSessions:)
的区别是,该方法仅在场景断开连接时调用,不会被丢弃,它可能会重新连接。而application(_: didDiscardSceneSessions:)
发生在使用应用程序切退出场景时。Info.plist文件文件包含App的配置信息,如App的名称,版本,支持的设备方向,现在我们可以通过配置Application Scene Manifest项来支持的不同场景。大多数应用程序只有一个场景,但是可以通过配置该项创建更多场景,如用于响应推送通知或特定操作的特定场景。
Enable Multiple Windows
: 默认为NO
,其设置为YES
可以支持多个窗口。Application Session Role
: 是一个数组,用于在应用程序中声明场景。 该数组每个元素是一个字典,字典中有三个键值,分别为
Configuration Name
: 当前配置的名字,必须是唯一的;
Delegate Class Name
: 场景的代理类名,将与该Scene代理对象关联;
StoryBoard name
: 场景用于创建初始UI的storyboard名称。application(_:configurationForConnecting:options:)
返回值为UISceneConfiguration
实例,上边三个键值分别对应UISceneConfiguration三个属性name
、delegateClass
、storyboard
。application(_:configurationForConnecting:options:)
方法也没有关系。如果没有在info.plist配置Application Scene Manifest项就需要实现这个方法并返回一个UISceneConfiguration对象。那么AppDelegate中的SceneDelegate
、UISceneSession
和Info.plist中的Application Scene Manifest
是如何一起创建多窗口应用的呢?
sceneDidBecomeActive(_:)
and sceneDidEnterBackground(_:)
之类的事件。Application Scene Manifest
列出了当前应用程序支持的场景,并将它们连接到delegate类并初始化storyboard。从iOS13开始AppDelegate不再有window属性,window属性被定义在SceneDelegate中。这是因为iOS13中AppDelegate的职责发现了改变:
因此,iOS13以前创建项目如果不需要多窗口就不需要任何改动,而iOS13以后创建新项目时,就要做一些适配:
如果使用纯代码来实现显示界面,需要在AppDelegate.h中手动添加window属性,添加以下代码即可:
class AppDelegate: UIResponder, UIApplicationDelegate {
//手动添加window属性
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame:UIScreen.main.bounds)
self.window!.backgroundColor = UIColor.white
//设置root
let rootVC = UIViewController()
self.window!.rootViewController = rootVC
self.window!.makeKeyAndVisible()
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>) {
// }
}
iOS 13后新项目中info.plist中的配置项Application Scene Manifest是针对iPad multiple windows功能推出的。在保留Application Scene Manifest配置项不予删除时(其中,项目是否支持多窗口功能是个可勾选项),AppDelegate的生命周期方法不再起作用,需要在SceneDelegate中使用UIScene提供的生命周期方法,并且需要针对 iOS 13 在Scene中配置和 iOS 13 以下在AppDelegate中做两套配置。
下面是纯代码实现界面显示的代码: Swift适配代码步骤:
//SceneDelegate.swift
@available(iOS 13, *) //在类的头部@available(iOS 13, *)添加即可
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
....
....
}
didFinishLaunchingWithOptions
中添加版本判断,AppDelegate中新增两个方法前添加@available(iOS 13, *)。也可以将这两个方法添加到AppDelegate分类中,分类前添加@available(iOS 13, *)。// AppDelegate.swift
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
//手动添加window属性
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 13, *) {
} else {
window = UIWindow(frame:UIScreen.main.bounds)
window!.backgroundColor = UIColor.blue
//设置root
let rootVC = UIViewController()
window!.rootViewController = rootVC
window!.makeKeyAndVisible()
}
return true
}
//新增方法添加@available(iOS 13, *)
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
}
}
@available(iOS 13, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
let vc = ViewController()
vc.view.backgroundColor = .red
let navigation = UINavigationController(rootViewController: vc)
window.rootViewController = navigation
window.makeKeyAndVisible()
self.window = window
}
...
...
}
OC适配代码:
// AppDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (@available(iOS 13.0, *)) {
} else {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window setBackgroundColor:[UIColor whiteColor]];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
}
return YES;
}
// SceneDelegate.m中
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
//在这里手动创建新的window
if (@available(iOS 13.0, *)) {
UIWindowScene *windowScene = (UIWindowScene *)scene;
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window setWindowScene:windowScene];
[self.window setBackgroundColor:[UIColor whiteColor]];
ViewController *con = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
}
}
func applicationWillResignActive(_ application: UIApplication) { }
...
...
SwiftUI创建的iOS 13项目,所以SwiftUI应用程序主要依靠SceneDelegate来设置应用程序的初始UI。
SwiftUI项目info.plist文件中Application Scene Manifest
项配置如下:
Enable Multiple Windows
设置为YES。//SceneDelegate.swift
import UIKit
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
.....
.....
}
上面的代码中发生了什么?
使用此方法可以有选择地配置UIWindow窗口并将其附加到提供的UIWindowScene场景。
如果使用storyboard,则window
属性将自动初始化并附加到场景中。
scene(_: willConnectTo: options:)
方法。 方法传入一个scene对象和一个session,传入的scene对象是由应用程序创建的。UIWindow
对象,但现在它们已成为scene(场景)的一部分。 在if let代码块中,使用scene来初始化UIWindow对象。将上边归纳如下内容:
scene(_: willConnectTo: options:)
被调用时,SceneDelegate会在正确的时间配置场景。scene(_: willConnectTo: options: )
函数内,创建一个SwiftUI视图,将其放置在托管控制器中,然后将控制器分配给window属性的根视图控制器,并将该窗口放置在应用程序UI的前面 。原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。