美文网首页
iOS13 获取根控制器

iOS13 获取根控制器

作者: 猫大人H | 来源:发表于2019-12-05 11:38 被阅读0次
+ (UIViewController *)hl_getRootViewController{
    UIWindow* window = nil;
       if (@available(iOS 13.0, *)) {
           for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes)
           {
              if (windowScene.activationState == UISceneActivationStateForegroundActive)
              {
             //实际上这一步是有问题的,应该增加判断 ishidden 
                   window = windowScene.windows.firstObject;
        
                   break;
              }
           }
       }else{
           #pragma clang diagnostic push
           #pragma clang diagnostic ignored "-Wdeprecated-declarations"
               // 这部分使用到的过期api
            window = [UIApplication sharedApplication].keyWindow;
           #pragma clang diagnostic pop
       }
    if([window.rootViewController isKindOfClass:NSNull.class]){
        return nil;
    }
    return window.rootViewController;
    }

推荐使用

+ (UIWindow *)keyWindow{
    UIWindow  *foundWindow = nil;
    NSArray   *windows = [[UIApplication sharedApplication]windows];
    for (UIWindow *window in windows) {
        if (window.isKeyWindow) {
            foundWindow = window;
            break;
        }
    }
    return foundWindow;
}

相关文章

网友评论

      本文标题:iOS13 获取根控制器

      本文链接:https://www.haomeiwen.com/subject/dirdgctx.html