(Objective-C) UIApplication获取win

2022-05-12  本文已影响0人  布呐呐u

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;
                }
            }
        }
    }
上一篇下一篇

猜你喜欢

热点阅读