处理再次进入弹起权限弹窗

This commit is contained in:
2025-11-03 15:04:19 +08:00
parent c7021e382e
commit e4cebeac85
6 changed files with 55 additions and 16 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -31,6 +31,11 @@
}];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
NSLog(@"===");
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// KBGuideVC *vc = [[KBGuideVC alloc] init];
// [self.navigationController pushViewController:vc animated:true];