[Swift] AppDelegate 與 SceneDelegate

先說結(jié)論:
SceneDelegate 用于 iPad 分屏 APP 開發(fā), 目前 iPhone 沒有分屏

純代碼, 不使用 StoryBoardxib

在 Xcode 11 和 iOS 13 中新增了 SceneDelegate, SceneDelegatescene 代替了 AppDelegatewindow

  • window 就是應(yīng)用程序, 只有一個, 所有操作, 都在 window 中
  • scene 場景, 應(yīng)用程序可以有多個場景, 每個場景都有一個 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上的多個窗口
支持應(yīng)用界面的并行實例并創(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 負(fù)責(zé) 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 文件, 管理場景的生命周期,處理各種響應(yīng)
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()
    }

    // 當(dāng)場景與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

  1. 刪除 SceneDelegate.swift
  2. 刪除 Info.plist 新增了 UIApplicationSceneManifest
  3. 刪除 AppDelegate.swift 中的 scene 相關(guān)方法
  4. 修改 LaunchScreen.storyboard 啟動頁面
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容