处理再次进入弹起权限弹窗
This commit is contained in:
@@ -10,6 +10,9 @@
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface UIViewController (Extension)
|
||||
/// Returns the top-most presented view controller from the app's active window.
|
||||
/// This mirrors the prior logic in AppDelegate (walks presentedViewController chain).
|
||||
+ (nullable UIViewController *)kb_topMostViewController;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
@@ -9,4 +9,38 @@
|
||||
|
||||
@implementation UIViewController (Extension)
|
||||
|
||||
/// Find the app's active window in a scene-friendly way (iOS 13+ safe)
|
||||
static inline __kindof UIWindow *KBActiveWindow(void) {
|
||||
UIWindow *window = [UIApplication sharedApplication].keyWindow;
|
||||
if (window) return window;
|
||||
// Fallbacks when keyWindow is nil (e.g. iOS 13+ with scenes)
|
||||
if (@available(iOS 13.0, *)) {
|
||||
for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
|
||||
if (scene.activationState != UISceneActivationStateForegroundActive) { continue; }
|
||||
if (![scene isKindOfClass:[UIWindowScene class]]) { continue; }
|
||||
UIWindowScene *ws = (UIWindowScene *)scene;
|
||||
for (UIWindow *w in ws.windows) {
|
||||
if (w.isKeyWindow) { return w; }
|
||||
}
|
||||
if (ws.windows.firstObject) { return ws.windows.firstObject; }
|
||||
}
|
||||
}
|
||||
// iOS 12 and earlier fallback
|
||||
for (UIWindow *w in [UIApplication sharedApplication].windows) {
|
||||
if (w.isKeyWindow) { return w; }
|
||||
}
|
||||
return [UIApplication sharedApplication].windows.firstObject;
|
||||
}
|
||||
|
||||
+ (UIViewController *)kb_topMostViewController {
|
||||
UIWindow *window = KBActiveWindow();
|
||||
UIViewController *root = window.rootViewController;
|
||||
if (!root) return nil;
|
||||
UIViewController *top = root;
|
||||
while (top.presentedViewController) {
|
||||
top = top.presentedViewController;
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user