Xcode11 新建工程运行黑屏解决方案

1、问题及原因

1.1、问题

   在升级到Xcode11及之后的版本,新建工程,直接给ViewController设置颜色或者直接运行,发现并不会执行ViewController当中的代码。

1.2、造成上述问题的原因

   Xcode11与之对应的是ios13系统。
   在我们更新到Xcode11与其之后的版本中,新建工程时我们会发现多出一个SceneDelegate 类,这是iPadOS用于支持多窗口。
   在iOS13之前Appdelegate全权管理App生命周期和UI生命周期。而iOS13之后,UI生命周期变成由SceneDelegate负责,而Appdelegate负责APP生命周期和SceneDelegate的生命周期。

2、解决方法

   根据我们项目是否需要支持多窗口,我们可以分为一下两种解决方案。

2.1、方案一(不支持多窗口)
  • 第一步,先将项目中的Info.plist文件中的Application Scene Manifest选项删除,然后删除SceneDelegate .hSceneDelegate.m文件(这两个文件删不删除没什么影响)。
  • 第二步,在AppDelegate .h文件中添加@property (nonatomic , strong) UIWindow *window;,然后在AppDelegate.m文件中的application: didFinishLaunchingWithOptions:方法中进行window的相关设置(Xcode11之前相应的设置)。
  • 第三步,将AppDelegate.m中一下代码删除或者注释掉。
#pragma mark - UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    // 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 {
    // 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.
}
2.2、方案二(支持多窗口)
  • 第一步,在SceneDelegate.m文件中进行window相应的设置(类似如下)。
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
    self.window.backgroundColor = UIColor.whiteColor;
    [self.window makeKeyAndVisible];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];
}
  • 第二步,完成上述操作只能保证项目在iOS13及之后能够正常运行,但iOS13一下还是不能正常运行的,所以我们还要进行iOS13一下系统的适配,首先在AppDelegate .h文件中添加@property (nonatomic , strong) UIWindow *window;,然后进行如下的判断和相应设置。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // @available(iOS 13.0,*)表示iOS 13.0及以上的系统,后面的*表示所有平台
    if (@available(iOS 13.0,*)) {
        
    }else{
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.window.backgroundColor = UIColor.whiteColor;
        [self.window makeKeyAndVisible];
        
        ViewController *vc = [ViewController new];
        
        self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:vc];
    }
    return YES;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容