先說結(jié)論:
SceneDelegate 用于 iPad 分屏 APP 開發(fā), 目前 iPhone 沒有分屏
純代碼, 不使用 StoryBoard
和 xib
在 Xcode 11 和 iOS 13 中新增了 SceneDelegate
, SceneDelegate
的 scene
代替了 AppDelegate
的 window
-
window
就是應用程序, 只有一個, 所有操作, 都在 window 中 -
scene
場景, 應用程序可以有多個場景, 每個場景都有一個 window
UISceneDelegate
Working with Window Scenes
Supporting Multiple Windows on iPad
Support side-by-side instances of your app’s interface and create new windows.
使用窗口場景
支持iPad上的多個窗口
支持應用界面的并行實例并創(chuàng)建新窗口。
AppDelegate (Xcode10 / iOS12 及之前)
AppDelegate
全權(quán)處理 App生命周期 和 UI生命周期
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let nav = UINavigationController(rootViewController: ViewController())
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = nav
window?.makeKeyAndVisible()
return true
}
// MARK: UIApplicationDelegate
@available(iOS 2.0, *)
optional func applicationDidBecomeActive(_ application: UIApplication)
@available(iOS 2.0, *)
optional func applicationWillResignActive(_ application: UIApplication)
@available(iOS 4.0, *)
optional func applicationDidEnterBackground(_ application: UIApplication)
@available(iOS 4.0, *)
optional func applicationWillEnterForeground(_ application: UIApplication)
AppDelegate + SceneDelegate (Xcode11 / iOS13)
-
AppDelegate
處理 App生命周期 和 SceneSession 生命周期 -
SceneSession
負責 UI生命周期
新項目變化
-
AppDelegate.swift
只配置UISceneConfiguration
, 不再管理window
,
// 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.
}
- 新增了
SceneDelegate.swift
文件, 管理場景的生命周期掂名,處理各種響應
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
// 與 didFinishLaunchingWithOptions 類似, 配置場景
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 windowScene: UIWindowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
self.window = window
let nav = UINavigationController(rootViewController: ViewController())
window.rootViewController = nav
window.makeKeyAndVisible()
}
// 當場景與app斷開連接 (還可能重新連接)
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 necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
// 與 AppDelegate 的 applicationDidBecomeActive 類似
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.
}
// 與 AppDelegate 的 applicationWillResignActive 類似
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).
}
// 與 AppDelegate 的 applicationWillEnterForeground 類似
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.
}
// 與 AppDelegate 的 applicationDidEnterBackground 類似
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.
}
}
-
Info.plist
新增了Application Scene Manifest
<plist version="1.0">
<dict>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
<key>UISceneStoryboardFile</key>
<string>Main</string>
</dict>
</array>
</dict>
</dict>
</dict>
</plist>
新項目刪除 SceneDelegate
- 刪除
SceneDelegate.swift
- 刪除
Info.plist
新增了UIApplicationSceneManifest
- 刪除
AppDelegate.swift
中的scene
相關(guān)方法 - 修改
LaunchScreen.storyboard
啟動頁面