iOS 13 [UIApplication sharedApplication].keyWindow 获取window适配

  UIWindow* window = nil;

        if (@available(iOS 13.0, *))
        {
            for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes)
            {
                if (windowScene.activationState == UISceneActivationStateForegroundActive)
                {
                    window = windowScene.windows.firstObject;

                    break;
                }
            }
        }else{
            window = [UIApplication sharedApplication].keyWindow;
        }
        

直接先献上代码,如上

2019.10.29

swift 版本

func keywindows() -> UIWindow? {

    var window:UIWindow? = nil

    if #available(iOS 13.0, *) {

        for windowScene:UIWindowScene in ((UIApplication.shared.connectedScenes as?  Set)!) {

            if windowScene.activationState == .foregroundActive {

                window = windowScene.windows.first

              break

            }

        }

        return window

    }else{

        return  UIApplication.shared.keyWindow

    }

}

 

你可能感兴趣的:(常识小结)