美文网首页
(Objective-C) UIApplication获取win

(Objective-C) UIApplication获取win

作者: 布呐呐u | 来源:发表于2022-05-12 00:26 被阅读0次

UIAlertView在iOS 9版本中已经废弃,无法在基于SceneDelegate管理的应用程序中使用

一)不使用SceneDelegate管理window

UIWindow *window = UIApplication.sharedApplication.delegate.window;

二)使用SceneDelegate管理window

  • 单一场景
UIWindow *window = ((id<UIWindowSceneDelegate>)[UIApplication sharedApplication].connectedScenes.anyObject.delegate).window;
  • 多个场景
NSDictionary *infoDic = NSBundle.mainBundle.infoDictionary[@"UIApplicationSceneManifest"];
    // 通过infoDic,判断是否使用UIScene
    if (infoDic) {
        // 通过supportsMultipleScenes,判断是否支持多个场景
        if (UIApplication.sharedApplication.supportsMultipleScenes) {
            // 获取所有已链接Scenes
            NSSet <UIScene *> *connectedScenes = [UIApplication sharedApplication].connectedScenes;
            for (UIScene *scene in (UIWindowScene *)connectedScenes.allObjects) {
                UISceneActivationState activationState = scene.activationState;
                // 通过activationState,获取当前场景window对象
                if (activationState == UISceneActivationStateForegroundActive) {
                    UIWindow *window =  ((id<UIWindowSceneDelegate>)scene.delegate).window;
                }
            }
        }
    }

相关文章

网友评论

      本文标题:(Objective-C) UIApplication获取win

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