diff --git a/keyBoard.xcodeproj/project.pbxproj b/keyBoard.xcodeproj/project.pbxproj index 89fe8e2..792db1c 100644 --- a/keyBoard.xcodeproj/project.pbxproj +++ b/keyBoard.xcodeproj/project.pbxproj @@ -11,7 +11,6 @@ 04A9FE0F2EB481100020DB6D /* KBHUD.m in Sources */ = {isa = PBXBuildFile; fileRef = 04FC97082EB31B14007BD342 /* KBHUD.m */; }; 04A9FE132EB4D0D20020DB6D /* KBFullAccessManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 04A9FE112EB4D0D20020DB6D /* KBFullAccessManager.m */; }; 04A9FE162EB873C80020DB6D /* UIViewController+Extension.m in Sources */ = {isa = PBXBuildFile; fileRef = 04A9FE152EB873C80020DB6D /* UIViewController+Extension.m */; }; - 04A9FE172EB873C80020DB6D /* UIViewController+Extension.m in Sources */ = {isa = PBXBuildFile; fileRef = 04A9FE152EB873C80020DB6D /* UIViewController+Extension.m */; }; 04C6EABA2EAF86530089C901 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 04C6EAAE2EAF86530089C901 /* Assets.xcassets */; }; 04C6EABC2EAF86530089C901 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 04C6EAB12EAF86530089C901 /* LaunchScreen.storyboard */; }; 04C6EABD2EAF86530089C901 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 04C6EAB42EAF86530089C901 /* Main.storyboard */; }; @@ -796,7 +795,6 @@ 04FC95B22EB0B2CC007BD342 /* KBSettingView.m in Sources */, 04FC95702EB09516007BD342 /* KBFunctionView.m in Sources */, 04FC956D2EB054B7007BD342 /* KBKeyboardView.m in Sources */, - 04A9FE172EB873C80020DB6D /* UIViewController+Extension.m in Sources */, 04FC95672EB0546C007BD342 /* KBKey.m in Sources */, A1B2C3F42EB35A9900000001 /* KBFullAccessGuideView.m in Sources */, A1B2C4002EB4A0A100000003 /* KBAuthManager.m in Sources */, diff --git a/keyBoard/AppDelegate.m b/keyBoard/AppDelegate.m index 77dd1e6..1b7ebd8 100644 --- a/keyBoard/AppDelegate.m +++ b/keyBoard/AppDelegate.m @@ -37,7 +37,7 @@ static NSString * const kKBKeyboardExtensionBundleId = @"com.keyBoardst.CustomKe [Bugly startWithAppId:BuglyId config:buglyConfig]; /// 判断获取键盘权限(统一管理): dispatch_async(dispatch_get_main_queue(), ^{ - UIViewController *top = [self kb_topMostViewController]; + UIViewController *top = [UIViewController kb_topMostViewController]; if (top) { Class mgrCls = NSClassFromString(@"KBKeyboardPermissionManager"); if (mgrCls && [mgrCls respondsToSelector:@selector(shared)]) { @@ -56,6 +56,15 @@ static NSString * const kKBKeyboardExtensionBundleId = @"com.keyBoardst.CustomKe return YES; } +- (void)applicationDidBecomeActive:(UIApplication *)application{ + // When returning from Settings, re-check the keyboard enable state + // and hide the guide if the user has enabled our keyboard. + // Also shows the guide again if still not enabled. + dispatch_async(dispatch_get_main_queue(), ^{ + [self kb_presentPermissionIfNeeded]; + }); +} + - (void)setupRootVC{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; @@ -67,17 +76,6 @@ static NSString * const kKBKeyboardExtensionBundleId = @"com.keyBoardst.CustomKe #pragma mark - Permission presentation -- (UIViewController *)kb_topMostViewController -{ - UIViewController *root = self.window.rootViewController; - if (!root) return nil; - UIViewController *top = root; - while (top.presentedViewController) { - top = top.presentedViewController; - } - return top; -} - #pragma mark - Deep Link @@ -103,7 +101,7 @@ static NSString * const kKBKeyboardExtensionBundleId = @"com.keyBoardst.CustomKe // 已登录则不提示 BOOL loggedIn = ([AppleSignInManager shared].storedUserIdentifier.length > 0); if (loggedIn) return; - UIViewController *top = [self kb_topMostViewController]; + UIViewController *top = [UIViewController kb_topMostViewController]; if (!top) return; [KBLoginSheetViewController presentIfNeededFrom:top]; } @@ -123,7 +121,7 @@ static NSString * const kKBKeyboardExtensionBundleId = @"com.keyBoardst.CustomKe - (void)kb_presentPermissionIfNeeded { BOOL enabled = KBIsKeyboardEnabled(); - UIViewController *top = [self kb_topMostViewController]; + UIViewController *top = [UIViewController kb_topMostViewController]; if (!top) return; if ([top isKindOfClass:[KBPermissionViewController class]]) { if (enabled) { diff --git a/keyBoard/Class/Categories/UIViewController+Extension.h b/keyBoard/Class/Categories/UIViewController+Extension.h index 281f13b..ab29df4 100644 --- a/keyBoard/Class/Categories/UIViewController+Extension.h +++ b/keyBoard/Class/Categories/UIViewController+Extension.h @@ -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 diff --git a/keyBoard/Class/Categories/UIViewController+Extension.m b/keyBoard/Class/Categories/UIViewController+Extension.m index 68b4ef2..de0def3 100644 --- a/keyBoard/Class/Categories/UIViewController+Extension.m +++ b/keyBoard/Class/Categories/UIViewController+Extension.m @@ -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 diff --git a/keyBoard/Class/Home/VC/HomeVC.m b/keyBoard/Class/Home/VC/HomeVC.m index 8a54048..5959460 100644 --- a/keyBoard/Class/Home/VC/HomeVC.m +++ b/keyBoard/Class/Home/VC/HomeVC.m @@ -31,6 +31,11 @@ }]; } +- (void)viewWillAppear:(BOOL)animated{ + [super viewWillAppear:animated]; + NSLog(@"==="); +} + - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // KBGuideVC *vc = [[KBGuideVC alloc] init]; // [self.navigationController pushViewController:vc animated:true]; diff --git a/keyBoard/KeyBoardPrefixHeader.pch b/keyBoard/KeyBoardPrefixHeader.pch index 24814f2..ece6649 100644 --- a/keyBoard/KeyBoardPrefixHeader.pch +++ b/keyBoard/KeyBoardPrefixHeader.pch @@ -25,6 +25,7 @@ /// 项目 +#import "UIViewController+Extension.h" #import "BaseNavigationController.h" #import "BaseTableView.h" #import "BaseCell.h"