iOS13 AppDelegate车遂、SceneDelegate兼容與適配

如果不適配就會(huì)導(dǎo)致iOS13以前的系統(tǒng)無法正確加載UIWindow而導(dǎo)致APP黑屏問題绢淀,如下是保留SceneDelegate且兼容iOS13以前系統(tǒng)的方法

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

/// 兼容iOS13之前的版本
@property (strong, nonatomic) UIWindow *window;

@end


AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    if (@available(iOS 13.0, *))
    {
        // 在SceneDelegate里創(chuàng)建UIWindow
    }
    else
    {
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [self.window setBackgroundColor:[UIColor whiteColor]];
        
        NSString *mainStoryboardFileName    =
        [[NSBundle mainBundle].infoDictionary valueForKey:@"UIMainStoryboardFile"];
        
        UIStoryboard *mainStoryboard        =
        [UIStoryboard storyboardWithName:mainStoryboardFileName
                                  bundle:[NSBundle mainBundle]];
        
        [self.window setRootViewController:[mainStoryboard instantiateInitialViewController]];
        [self.window makeKeyAndVisible];
    }
    return YES;
}

#pragma mark - 兼容iOS13之前的版本
- (void)applicationWillResignActive:(UIApplication *)application
{
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    
    
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    
    
}

#pragma mark - iOS13 以后 UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options  API_AVAILABLE(ios(13.0)){
    // Called when a new scene session is being created.
    // Use this method to select a configuration to create the new scene with.
    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}


- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions  API_AVAILABLE(ios(13.0)){
    // 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.
}

@end


SceneDelegate.h

#import <UIKit/UIKit.h>

@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>

@property (strong, nonatomic) UIWindow * window;

@end


SceneDelegate.m

#import "SceneDelegate.h"

@interface SceneDelegate ()

@end

@implementation SceneDelegate


- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions  API_AVAILABLE(ios(13.0)){
    // 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).
    
    /*
    // 在這里手動(dòng)創(chuàng)建新的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 systemBackgroundColor]];
        
        NSString *mainStoryboardFileName    =
        [[NSBundle mainBundle].infoDictionary valueForKey:@"UIMainStoryboardFile"];
        
        UIStoryboard *mainStoryboard        =
        [UIStoryboard storyboardWithName:mainStoryboardFileName
                                  bundle:[NSBundle mainBundle]];
        
        [self.window setRootViewController:[mainStoryboard instantiateInitialViewController]];
        [self.window makeKeyAndVisible];
    }
    else
    {
        // 在AppDelegate里創(chuàng)建UIWindow
    }
     */
}


- (void)sceneDidDisconnect:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    // 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).
}


- (void)sceneDidBecomeActive:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    // 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.
}


- (void)sceneWillResignActive:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    // 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).
}


- (void)sceneWillEnterForeground:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    // Called as the scene transitions from the background to the foreground.
    // Use this method to undo the changes made on entering the background.
}


- (void)sceneDidEnterBackground:(UIScene *)scene  API_AVAILABLE(ios(13.0)){
    // 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.
}


@end
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蚁廓,一起剝皮案震驚了整個(gè)濱河市陌选,隨后出現(xiàn)的幾起案子睡汹,更是在濱河造成了極大的恐慌肴甸,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,331評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件囚巴,死亡現(xiàn)場離奇詭異原在,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)文兢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,372評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門晤斩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人姆坚,你說我怎么就攤上這事∈涤蓿” “怎么了兼呵?”我有些...
    開封第一講書人閱讀 167,755評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵兔辅,是天一觀的道長。 經(jīng)常有香客問我击喂,道長维苔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,528評(píng)論 1 296
  • 正文 為了忘掉前任懂昂,我火速辦了婚禮介时,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘凌彬。我一直安慰自己沸柔,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,526評(píng)論 6 397
  • 文/花漫 我一把揭開白布铲敛。 她就那樣靜靜地躺著褐澎,像睡著了一般。 火紅的嫁衣襯著肌膚如雪伐蒋。 梳的紋絲不亂的頭發(fā)上工三,一...
    開封第一講書人閱讀 52,166評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音先鱼,去河邊找鬼俭正。 笑死,一個(gè)胖子當(dāng)著我的面吹牛焙畔,可吹牛的內(nèi)容都是我干的段审。 我是一名探鬼主播,決...
    沈念sama閱讀 40,768評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼闹蒜,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼寺枉!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起绷落,我...
    開封第一講書人閱讀 39,664評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤姥闪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后砌烁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體筐喳,經(jīng)...
    沈念sama閱讀 46,205評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,290評(píng)論 3 340
  • 正文 我和宋清朗相戀三年函喉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了避归。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,435評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡管呵,死狀恐怖梳毙,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情捐下,我是刑警寧澤账锹,帶...
    沈念sama閱讀 36,126評(píng)論 5 349
  • 正文 年R本政府宣布萌业,位于F島的核電站,受9級(jí)特大地震影響奸柬,放射性物質(zhì)發(fā)生泄漏生年。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,804評(píng)論 3 333
  • 文/蒙蒙 一廓奕、第九天 我趴在偏房一處隱蔽的房頂上張望抱婉。 院中可真熱鬧,春花似錦桌粉、人聲如沸蒸绩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,276評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽侵贵。三九已至,卻和暖如春缘薛,著一層夾襖步出監(jiān)牢的瞬間窍育,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來泰國打工宴胧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留漱抓,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,818評(píng)論 3 376
  • 正文 我出身青樓恕齐,卻偏偏與公主長得像乞娄,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子显歧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,442評(píng)論 2 359