關(guān)于iOS 13 SceneDelegate 適配

在XCode11 之后,在我們新建工程的時(shí)候發(fā)現(xiàn)多了一個(gè)新增文件 SceneDelegate.swift 憨颠,然后我們?nèi)タ聪逻@個(gè)文件到底是干什么用的WWDC2019:Optimizing App Launch在iOS 13之前,APP的生命周期和UI生命周期都是交由AppDelegate來(lái)處理的恬汁,在iOS 13以前谤逼,處理生命周期是下面這樣的。

AppDelegate.png

這種模式是基于只有一個(gè)進(jìn)程時(shí)適用戈盈,因?yàn)閕OS一直以來(lái)都是一個(gè)進(jìn)程運(yùn)行時(shí)只有一個(gè)用戶(hù)界面奠衔。在iOS 13之后,其實(shí)是為了iPadOS的多窗口支持塘娶,它可以高效的把應(yīng)用委托工作分成兩部分归斤。
SceneDelegate承接了部分工作

所以我們看到的是這樣承接了業(yè)務(wù):

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

這里是通過(guò)了func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration
把內(nèi)容連接到了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這么高的版本血柳,并且開(kāi)發(fā)的應(yīng)用程序也沒(méi)有兼容iPadOS(很多時(shí)候官册,iPadOS版本的還是需要另外做個(gè)版本來(lái)適配),所以這里提出了兩個(gè)方案來(lái)進(jìn)行適配

1难捌、添加版本判別運(yùn)行代碼@available

在使用 @available(iOS 13.0, *)進(jìn)行判斷膝宁,如果系統(tǒng)沒(méi)到達(dá)13.0的話(huà)就不啟用該段代碼鸦难。

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

不過(guò),還需要在AppDelegate上添加一個(gè)window才可以保證正常運(yùn)行

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相關(guān)內(nèi)容

這是基于不需要使用多窗口支持而提出的解決方案合蔽,首先,需要去掉info.plist 的Application Scene Manifest選項(xiàng)介返,然后對(duì)應(yīng)的SceneDelegate刪除掉拴事。
然后把對(duì)應(yīng)的代碼內(nèi)容也注釋掉或者刪掉對(duì)應(yīng)的內(nèi)容,然后寫(xiě)上對(duì)應(yīng)的生命周期圣蝎,如下:

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


}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末刃宵,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子徘公,更是在濱河造成了極大的恐慌牲证,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件关面,死亡現(xiàn)場(chǎng)離奇詭異坦袍,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)等太,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門(mén)捂齐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人缩抡,你說(shuō)我怎么就攤上這事奠宜。” “怎么了瞻想?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵挎塌,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我内边,道長(zhǎng)榴都,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任漠其,我火速辦了婚禮嘴高,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘和屎。我一直安慰自己拴驮,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布柴信。 她就那樣靜靜地躺著套啤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪随常。 梳的紋絲不亂的頭發(fā)上潜沦,一...
    開(kāi)封第一講書(shū)人閱讀 52,156評(píng)論 1 308
  • 那天萄涯,我揣著相機(jī)與錄音,去河邊找鬼唆鸡。 笑死涝影,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的争占。 我是一名探鬼主播燃逻,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼臂痕!你這毒婦竟也來(lái)了伯襟?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤握童,失蹤者是張志新(化名)和其女友劉穎逗旁,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體舆瘪,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年红伦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了英古。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡昙读,死狀恐怖召调,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蛮浑,我是刑警寧澤唠叛,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站沮稚,受9級(jí)特大地震影響艺沼,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蕴掏,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一障般、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧盛杰,春花似錦挽荡、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至逗嫡,卻和暖如春青自,著一層夾襖步出監(jiān)牢的瞬間株依,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工性穿, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留勺三,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓需曾,卻偏偏與公主長(zhǎng)得像吗坚,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子呆万,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容