首页 > ios 低概率crash Application windows are expected to have a root

ios 低概率crash Application windows are expected to have a root

这个错误我也是了解的 但是自己亲自调试从来没遇见过 上线后 基本几千个用户用可能才会出现一次 现在已经被crash了3、4次了 不知道是为什么 有人知道这种错误吗 出现概率极低 但是确实会出现 我看了与机型、系统版本都无关 以下是didFinishLaunchingWithOptions进入controller的代码

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIViewController *viewCtl;
    if ([LocalStore getIsLogin] == YES) {
        viewCtl = [[MainViewController alloc] init];
    }else{
        if ([LocalStore getIsScreen] == YES) {
            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"GuidePage" bundle:nil];
            GuidePageViewController *guide = [storyBoard instantiateViewControllerWithIdentifier:@"guidePage"];
            viewCtl = [[UINavigationController alloc] initWithRootViewController:guide];
            [guide setNavigationController:(UINavigationController *)viewCtl];
        }else{
            viewCtl = [[ScreenViewController alloc] init];
        }
    }
    
    self.window.rootViewController = viewCtl;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    

window 需要一个rootviewcontroller
有可能的window还没创建的时候 调用了别的代码(例如闪屏广告,自定义的弹窗等) 而别的代码实现里里生成了window 这个时候就会到这这种崩溃。通用做法是生成一个空的viewcontroller然后给这个window赋值。


尝试在启动的时候就设置好window和rootViewController,然后再进行其他操作呢,完成之后再替换rootViewController。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[UIViewController alloc] init];
    [self.window makeKeyAndVisible];
    ...    //其他操作 
    //完成之后再替换掉
    self.window.rootViewController = someVC;
}
【热门文章】
【热门文章】