2025-10-27 18:47:18 +08:00
|
|
|
|
//
|
|
|
|
|
|
// AppDelegate.m
|
|
|
|
|
|
// keyBoard
|
|
|
|
|
|
//
|
|
|
|
|
|
// Created by 张伟 on 2025/10/27.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#import "AppDelegate.h"
|
2025-11-21 18:26:02 +08:00
|
|
|
|
//#import "KBPermissionViewController.h"
|
2025-10-27 21:55:05 +08:00
|
|
|
|
#import <AFNetworking/AFNetworking.h>
|
2025-11-03 21:04:39 +08:00
|
|
|
|
#if !DEBUG
|
2025-10-27 21:55:05 +08:00
|
|
|
|
#import <Bugly/Bugly.h>
|
2025-11-03 21:04:39 +08:00
|
|
|
|
#endif
|
2025-10-29 14:17:26 +08:00
|
|
|
|
#import "BaseTabBarController.h"
|
2025-11-21 18:26:02 +08:00
|
|
|
|
//#import "LoginViewController.h"
|
|
|
|
|
|
//#import "KBLoginSheetViewController.h"
|
|
|
|
|
|
//#import "AppleSignInManager.h"
|
|
|
|
|
|
//#import <objc/message.h>
|
|
|
|
|
|
#import "KBULBridgeNotification.h" // Darwin 通知常量:用于通知扩展“UL已处理”
|
2025-11-12 21:23:31 +08:00
|
|
|
|
#import "LSTPopView.h"
|
|
|
|
|
|
#import "KBLoginPopView.h"
|
2025-11-17 16:16:38 +08:00
|
|
|
|
#import "KBSexSelVC.h"
|
2025-11-24 19:58:19 +08:00
|
|
|
|
#import "KBKeyboardPermissionManager.h"
|
2026-02-04 21:49:28 +08:00
|
|
|
|
#import "KBPayMainVC.h"
|
2025-12-02 18:29:04 +08:00
|
|
|
|
#import "KBUserSessionManager.h"
|
|
|
|
|
|
#import "KBLoginVC.h"
|
2025-12-17 19:45:39 +08:00
|
|
|
|
#import "KBConfig.h"
|
2026-03-05 17:42:50 +08:00
|
|
|
|
#import "KBLocalizationManager.h"
|
2026-03-02 20:52:02 +08:00
|
|
|
|
#import "KBSkinInstallBridge.h"
|
2026-03-05 17:42:50 +08:00
|
|
|
|
#import "KBSkinManager.h"
|
2026-03-04 18:05:39 +08:00
|
|
|
|
#import "KBAppUpdateView.h"
|
2026-03-05 17:42:50 +08:00
|
|
|
|
#import "KBInputProfileManager.h"
|
2025-12-17 19:45:39 +08:00
|
|
|
|
|
|
|
|
|
|
static NSTimeInterval const kKBSubscriptionPrefillTTL = 10 * 60.0;
|
2025-11-17 18:51:06 +08:00
|
|
|
|
|
2025-10-30 18:31:12 +08:00
|
|
|
|
// 注意:用于判断系统已启用本输入法扩展的 bundle id 需与扩展 target 的
|
|
|
|
|
|
// PRODUCT_BUNDLE_IDENTIFIER 完全一致。
|
2025-11-03 21:04:39 +08:00
|
|
|
|
// 当前工程的 CustomKeyboard target 为 com.loveKey.nyx.CustomKeyboard
|
2025-11-21 18:26:02 +08:00
|
|
|
|
//static NSString * const kKBKeyboardExtensionBundleId = @"com.loveKey.nyx.CustomKeyboard";
|
2025-10-27 18:47:18 +08:00
|
|
|
|
|
2026-03-04 18:05:39 +08:00
|
|
|
|
@interface AppDelegate () <KBAppUpdateViewDelegate>
|
|
|
|
|
|
@property (nonatomic, strong) LSTPopView *appUpdatePopView;
|
2026-03-04 20:36:53 +08:00
|
|
|
|
@property (nonatomic, strong) KBAppUpdateInfo *appUpdateInfo;
|
2026-03-04 18:05:39 +08:00
|
|
|
|
@end
|
|
|
|
|
|
|
2025-10-27 18:47:18 +08:00
|
|
|
|
@implementation AppDelegate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
2025-12-05 13:49:12 +08:00
|
|
|
|
/// 1:配置国际化(统一使用集中管理的语言列表)
|
2025-12-02 20:33:17 +08:00
|
|
|
|
[KBLocalizationManager shared].supportedLanguageCodes = KBDefaultSupportedLanguageCodes();
|
2026-03-05 17:42:50 +08:00
|
|
|
|
/// 1.1:首次启动为键盘写入默认输入配置(语言/布局/profile),保证“App 语言=键盘语言”
|
|
|
|
|
|
[self kb_bootstrapDefaultKeyboardProfileIfNeeded];
|
|
|
|
|
|
/// 1.2:未手动选择键盘输入配置时,让键盘配置随 App 语言变化
|
|
|
|
|
|
[self kb_syncKeyboardProfileToCurrentAppLanguageIfNeeded];
|
2025-12-05 13:49:12 +08:00
|
|
|
|
/// 2 : 处理token问题(包括卸载重装场景下的 token 清理)
|
2025-12-03 12:55:51 +08:00
|
|
|
|
[[KBUserSessionManager shared] bootstrapIfNeeded];
|
2025-12-02 18:29:04 +08:00
|
|
|
|
|
2025-11-24 19:58:19 +08:00
|
|
|
|
// 首次安装/升级:重置“完全访问”记录,避免继承旧安装遗留在 Keychain 中的状态
|
|
|
|
|
|
static NSString *const kKBFullAccessRecordInitializedKey = @"KBFullAccessRecordInitialized";
|
|
|
|
|
|
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
|
if (![ud boolForKey:kKBFullAccessRecordInitializedKey]) {
|
|
|
|
|
|
[[KBKeyboardPermissionManager shared] resetFullAccessRecord];
|
|
|
|
|
|
[ud setBool:YES forKey:kKBFullAccessRecordInitializedKey];
|
|
|
|
|
|
[ud synchronize];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-17 16:16:38 +08:00
|
|
|
|
// 首次安装先进入性别选择页;点击 Skip 或确认后再进入主 TabBar
|
2025-12-03 12:55:51 +08:00
|
|
|
|
BOOL hasShownSexVC = [[NSUserDefaults standardUserDefaults] boolForKey:KBSexSelectShownKey];
|
|
|
|
|
|
if (hasShownSexVC) {
|
|
|
|
|
|
[self setupRootVC];
|
|
|
|
|
|
} else {
|
2025-11-17 16:16:38 +08:00
|
|
|
|
[self setupSexSelectRootVC];
|
2025-12-03 12:55:51 +08:00
|
|
|
|
}
|
2025-11-17 16:16:38 +08:00
|
|
|
|
|
2025-10-30 13:10:33 +08:00
|
|
|
|
// 主工程默认开启网络总开关(键盘扩展仍需用户允许完全访问后再行开启)
|
|
|
|
|
|
[KBNetworkManager shared].enabled = YES;
|
2025-10-27 21:55:05 +08:00
|
|
|
|
/// 获取网络权限
|
|
|
|
|
|
[self getNetJudge];
|
2026-03-02 20:52:02 +08:00
|
|
|
|
|
|
|
|
|
|
// 安装默认皮肤(首次安装时)
|
|
|
|
|
|
[self kb_installDefaultSkinIfNeeded];
|
2026-03-05 17:42:50 +08:00
|
|
|
|
|
|
|
|
|
|
// App 语言变化(系统跟随或手动切换)时,同步键盘配置并让键盘即时刷新
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
|
|
selector:@selector(kb_onLocalizationChanged:)
|
|
|
|
|
|
name:KBLocalizationDidChangeNotification
|
|
|
|
|
|
object:nil];
|
2025-11-03 21:04:39 +08:00
|
|
|
|
|
2026-03-04 18:05:39 +08:00
|
|
|
|
[self kb_requestAppUpdateAndPresentIfNeeded];
|
|
|
|
|
|
|
2025-11-03 21:04:39 +08:00
|
|
|
|
#if !DEBUG
|
|
|
|
|
|
/// Bugly
|
2025-10-27 22:02:46 +08:00
|
|
|
|
BuglyConfig *buglyConfig = [BuglyConfig new];
|
|
|
|
|
|
/// 设置GroupID进行配置
|
|
|
|
|
|
// buglyConfig.applicationGroupIdentifier = @"";
|
|
|
|
|
|
[Bugly startWithAppId:BuglyId config:buglyConfig];
|
2025-11-03 21:04:39 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-10-27 18:47:18 +08:00
|
|
|
|
return YES;
|
|
|
|
|
|
}
|
2025-10-27 21:11:28 +08:00
|
|
|
|
|
2026-03-05 17:42:50 +08:00
|
|
|
|
- (NSString *)kb_defaultKeyboardLanguageCodeForAppLanguageCode:(NSString *)appLanguageCode {
|
|
|
|
|
|
NSString *lc = appLanguageCode.lowercaseString ?: @"";
|
|
|
|
|
|
if ([lc hasPrefix:@"es"]) { return @"es"; }
|
|
|
|
|
|
if ([lc hasPrefix:@"pt"]) { return @"pt"; }
|
|
|
|
|
|
if ([lc hasPrefix:@"id"]) { return @"id"; }
|
|
|
|
|
|
if ([lc hasPrefix:@"zh-hant"]) { return @"zh-Hant-Pinyin"; }
|
|
|
|
|
|
return @"en";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)kb_bootstrapDefaultKeyboardProfileIfNeeded {
|
|
|
|
|
|
NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:AppGroup];
|
|
|
|
|
|
NSString *savedProfileId = [shared stringForKey:AppGroup_SelectedKeyboardProfileId];
|
|
|
|
|
|
if (savedProfileId.length > 0) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NSString *appLanguageCode = [KBLocalizationManager shared].currentLanguageCode ?: KBLanguageCodeEnglish;
|
|
|
|
|
|
NSString *kbLanguageCode = [self kb_defaultKeyboardLanguageCodeForAppLanguageCode:appLanguageCode];
|
|
|
|
|
|
KBInputProfile *profile = [[KBInputProfileManager sharedManager] profileForLanguageCode:kbLanguageCode];
|
|
|
|
|
|
if (!profile) {
|
|
|
|
|
|
profile = [[KBInputProfileManager sharedManager] profileForLanguageCode:@"en"];
|
|
|
|
|
|
}
|
|
|
|
|
|
KBInputProfileLayout *layout = profile.layouts.firstObject;
|
|
|
|
|
|
if (!layout) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[shared setObject:(profile.code ?: @"en") forKey:AppGroup_SelectedKeyboardLanguageCode];
|
|
|
|
|
|
[shared setObject:(layout.variant ?: @"qwerty") forKey:AppGroup_SelectedKeyboardLayoutVariant];
|
|
|
|
|
|
[shared setObject:(layout.profileId ?: @"en_US_qwerty") forKey:AppGroup_SelectedKeyboardProfileId];
|
|
|
|
|
|
[shared synchronize];
|
|
|
|
|
|
|
|
|
|
|
|
NSLog(@"[AppDelegate] Bootstrap keyboard profile: appLang=%@ kbLang=%@ profileId=%@ variant=%@",
|
|
|
|
|
|
appLanguageCode, profile.code, layout.profileId, layout.variant);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)kb_syncKeyboardProfileToCurrentAppLanguageIfNeeded {
|
|
|
|
|
|
NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:AppGroup];
|
|
|
|
|
|
if ([shared boolForKey:AppGroup_DidUserSelectKeyboardProfile]) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
NSString *appLanguageCode = [KBLocalizationManager shared].currentLanguageCode ?: KBLanguageCodeEnglish;
|
|
|
|
|
|
NSString *kbLanguageCode = [self kb_defaultKeyboardLanguageCodeForAppLanguageCode:appLanguageCode];
|
|
|
|
|
|
|
|
|
|
|
|
KBInputProfile *profile = [[KBInputProfileManager sharedManager] profileForLanguageCode:kbLanguageCode];
|
|
|
|
|
|
if (!profile) {
|
|
|
|
|
|
profile = [[KBInputProfileManager sharedManager] profileForLanguageCode:@"en"];
|
|
|
|
|
|
}
|
|
|
|
|
|
KBInputProfileLayout *layout = profile.layouts.firstObject;
|
|
|
|
|
|
if (!layout) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NSString *curProfileId = [shared stringForKey:AppGroup_SelectedKeyboardProfileId] ?: @"";
|
|
|
|
|
|
NSString *curLang = [shared stringForKey:AppGroup_SelectedKeyboardLanguageCode] ?: @"";
|
|
|
|
|
|
NSString *curVariant = [shared stringForKey:AppGroup_SelectedKeyboardLayoutVariant] ?: @"";
|
|
|
|
|
|
if ([curProfileId isEqualToString:layout.profileId ?: @""] &&
|
|
|
|
|
|
[curLang isEqualToString:profile.code ?: @""] &&
|
|
|
|
|
|
[curVariant isEqualToString:layout.variant ?: @""]) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[shared setObject:(profile.code ?: @"en") forKey:AppGroup_SelectedKeyboardLanguageCode];
|
|
|
|
|
|
[shared setObject:(layout.variant ?: @"qwerty") forKey:AppGroup_SelectedKeyboardLayoutVariant];
|
|
|
|
|
|
[shared setObject:(layout.profileId ?: @"en_US_qwerty") forKey:AppGroup_SelectedKeyboardProfileId];
|
|
|
|
|
|
[shared synchronize];
|
|
|
|
|
|
|
|
|
|
|
|
NSLog(@"[AppDelegate] Sync keyboard profile to appLang=%@ kbLang=%@ profileId=%@ variant=%@",
|
|
|
|
|
|
appLanguageCode, profile.code, layout.profileId, layout.variant);
|
|
|
|
|
|
|
|
|
|
|
|
// 同步完键盘语言后,如当前是默认皮肤,则让皮肤也跟随(避免 key_icons 仍是旧语言导致看起来“还是繁体”)
|
|
|
|
|
|
[self kb_applyDefaultSkinForKeyboardLanguageIfNeeded:profile.code ?: @"en"];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)kb_onLocalizationChanged:(NSNotification *)note {
|
|
|
|
|
|
[self kb_syncKeyboardProfileToCurrentAppLanguageIfNeeded];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)kb_applyDefaultSkinForKeyboardLanguageIfNeeded:(NSString *)languageCode {
|
|
|
|
|
|
NSString *lc = languageCode.length > 0 ? languageCode : @"en";
|
|
|
|
|
|
NSString *targetSkinId = [NSString stringWithFormat:@"bundle_skin_default_%@", lc];
|
|
|
|
|
|
NSString *currentSkinId = [KBSkinManager shared].current.skinId ?: @"";
|
|
|
|
|
|
BOOL isDefaultLike = (currentSkinId.length == 0 ||
|
|
|
|
|
|
[currentSkinId isEqualToString:@"default"] ||
|
|
|
|
|
|
[currentSkinId hasPrefix:@"bundle_skin_default_"]);
|
|
|
|
|
|
if (!isDefaultLike) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ([currentSkinId isEqualToString:targetSkinId] && [KBSkinManager kb_hasAssetsForSkinId:targetSkinId]) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
KBInputProfile *profile = [[KBInputProfileManager sharedManager] profileForLanguageCode:lc];
|
|
|
|
|
|
NSString *zipName = profile.defaultSkinZip.length > 0 ? profile.defaultSkinZip : @"normal_them.zip";
|
|
|
|
|
|
NSDictionary<NSString *, NSString *> *iconShortNames = [KBSkinInstallBridge iconShortNamesForLanguageCode:lc];
|
|
|
|
|
|
|
|
|
|
|
|
NSLog(@"[AppDelegate] Applying default skin for keyboard language=%@ currentSkin=%@ targetSkin=%@ zip=%@",
|
|
|
|
|
|
lc, currentSkinId, targetSkinId, zipName);
|
|
|
|
|
|
|
|
|
|
|
|
[KBSkinInstallBridge publishBundleSkinRequestWithId:targetSkinId
|
|
|
|
|
|
name:targetSkinId
|
|
|
|
|
|
zipName:zipName
|
|
|
|
|
|
iconShortNames:iconShortNames];
|
|
|
|
|
|
[KBSkinInstallBridge consumePendingRequestFromBundle:[NSBundle mainBundle]
|
|
|
|
|
|
completion:^(BOOL success, NSError * _Nullable error) {
|
|
|
|
|
|
if (!success) {
|
|
|
|
|
|
NSLog(@"[AppDelegate] Apply default skin failed: %@", error);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
NSLog(@"[AppDelegate] Apply default skin success: %@", targetSkinId);
|
|
|
|
|
|
}
|
|
|
|
|
|
}];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-04 18:05:39 +08:00
|
|
|
|
- (void)kb_requestAppUpdateAndPresentIfNeeded {
|
2026-03-04 19:49:07 +08:00
|
|
|
|
__weak typeof(self) weakSelf = self;
|
2026-03-04 20:36:53 +08:00
|
|
|
|
[[KBLoginVM shared] checkAppUpdateWithCompletion:^(KBAppUpdateInfo * _Nullable info, NSError * _Nullable error) {
|
2026-03-04 19:49:07 +08:00
|
|
|
|
if (error) { return; }
|
2026-03-04 20:36:53 +08:00
|
|
|
|
if (!info.needUpdate) { return; }
|
2026-03-04 19:49:07 +08:00
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2026-03-04 20:36:53 +08:00
|
|
|
|
[weakSelf kb_presentAppUpdateWithInfo:info];
|
2026-03-04 19:49:07 +08:00
|
|
|
|
});
|
|
|
|
|
|
}];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-04 20:36:53 +08:00
|
|
|
|
- (void)kb_presentAppUpdateWithInfo:(KBAppUpdateInfo *)info {
|
2026-03-04 19:49:07 +08:00
|
|
|
|
if (self.appUpdatePopView) { return; }
|
2026-03-04 20:36:53 +08:00
|
|
|
|
self.appUpdateInfo = info;
|
2026-03-04 19:49:07 +08:00
|
|
|
|
CGFloat width = KBFit(323.0);
|
|
|
|
|
|
CGFloat height = KBFit(390.0);
|
|
|
|
|
|
KBAppUpdateView *view = [[KBAppUpdateView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
|
|
|
|
|
|
view.backgroundImageName = @"app_update_bg";
|
|
|
|
|
|
view.delegate = self;
|
2026-03-04 20:36:53 +08:00
|
|
|
|
view.updateInfo = info;
|
2026-03-04 19:49:07 +08:00
|
|
|
|
|
|
|
|
|
|
LSTPopView *pop = [LSTPopView initWithCustomView:view
|
|
|
|
|
|
parentView:nil
|
|
|
|
|
|
popStyle:LSTPopStyleScale
|
|
|
|
|
|
dismissStyle:LSTDismissStyleScale];
|
|
|
|
|
|
pop.hemStyle = LSTHemStyleCenter;
|
|
|
|
|
|
pop.bgColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
|
|
|
|
|
|
pop.isClickBgDismiss = NO;
|
|
|
|
|
|
pop.cornerRadius = 0;
|
|
|
|
|
|
self.appUpdatePopView = pop;
|
|
|
|
|
|
[pop pop];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-04 18:05:39 +08:00
|
|
|
|
#pragma mark - KBAppUpdateViewDelegate
|
|
|
|
|
|
|
|
|
|
|
|
- (void)appUpdateViewDidTapUpgrade:(KBAppUpdateView *)view {
|
2026-03-04 20:36:53 +08:00
|
|
|
|
NSString *urlString = self.appUpdateInfo.downloadUrl;
|
2026-03-04 19:49:07 +08:00
|
|
|
|
if (urlString.length > 0) {
|
|
|
|
|
|
NSURL *url = [NSURL URLWithString:urlString];
|
|
|
|
|
|
if (url) {
|
|
|
|
|
|
UIApplication *app = [UIApplication sharedApplication];
|
|
|
|
|
|
if ([app canOpenURL:url]) {
|
|
|
|
|
|
if (@available(iOS 10.0, *)) {
|
|
|
|
|
|
[app openURL:url options:@{} completionHandler:nil];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
[app openURL:url];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-03-04 20:36:53 +08:00
|
|
|
|
// [self.appUpdatePopView dismiss];
|
|
|
|
|
|
// self.appUpdatePopView = nil;
|
|
|
|
|
|
// self.appUpdateInfo = nil;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)appUpdateViewDidTapCancel:(KBAppUpdateView *)view {
|
2026-03-04 18:05:39 +08:00
|
|
|
|
[self.appUpdatePopView dismiss];
|
|
|
|
|
|
self.appUpdatePopView = nil;
|
2026-03-04 20:36:53 +08:00
|
|
|
|
self.appUpdateInfo = nil;
|
2026-03-04 18:05:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-17 18:51:06 +08:00
|
|
|
|
- (void)applicationDidBecomeActive:(UIApplication *)application{
|
|
|
|
|
|
// [self kb_checkNetworkAndShowAlertIfNeeded];
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-11-03 15:04:19 +08:00
|
|
|
|
|
2025-10-29 14:17:26 +08:00
|
|
|
|
|
|
|
|
|
|
- (void)setupRootVC{
|
|
|
|
|
|
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
|
|
|
|
|
self.window.backgroundColor = [UIColor whiteColor];
|
|
|
|
|
|
[self.window makeKeyAndVisible];
|
2025-12-03 13:31:02 +08:00
|
|
|
|
|
|
|
|
|
|
// 根据当前是否已登录决定入口:有 token 进主 TabBar,否则先进入登录页
|
2025-12-03 14:30:02 +08:00
|
|
|
|
// BOOL loggedIn = [[KBUserSessionManager shared] isLoggedIn];
|
2025-12-03 13:31:02 +08:00
|
|
|
|
UIViewController *rootVC = nil;
|
2025-12-03 14:30:02 +08:00
|
|
|
|
|
2025-12-03 13:31:02 +08:00
|
|
|
|
rootVC = [[BaseTabBarController alloc] init];
|
2025-12-03 14:30:02 +08:00
|
|
|
|
|
|
|
|
|
|
self.window.rootViewController = rootVC;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)toMainTabbarVC{
|
|
|
|
|
|
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
|
|
|
|
|
self.window.backgroundColor = [UIColor whiteColor];
|
|
|
|
|
|
[self.window makeKeyAndVisible];
|
|
|
|
|
|
UIViewController *rootVC = nil;
|
|
|
|
|
|
rootVC = [[BaseTabBarController alloc] init];
|
2025-12-03 13:31:02 +08:00
|
|
|
|
self.window.rootViewController = rootVC;
|
2025-10-29 14:17:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-17 16:16:38 +08:00
|
|
|
|
/// 首次安装时,展示性别选择页作为根控制器
|
|
|
|
|
|
- (void)setupSexSelectRootVC {
|
2025-12-03 13:31:02 +08:00
|
|
|
|
KBSexSelVC *vc = [[KBSexSelVC alloc] init];
|
2025-11-17 16:16:38 +08:00
|
|
|
|
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
|
|
|
|
|
self.window.backgroundColor = [UIColor whiteColor];
|
|
|
|
|
|
[self.window makeKeyAndVisible];
|
2025-12-02 18:29:04 +08:00
|
|
|
|
self.window.rootViewController = vc;
|
2025-11-17 16:16:38 +08:00
|
|
|
|
KBSexSelVC *sexVC = [KBSexSelVC new];
|
|
|
|
|
|
__weak typeof(self) weakSelf = self;
|
|
|
|
|
|
sexVC.didFinishSelectBlock = ^{
|
|
|
|
|
|
// 记录已经展示过性别选择页,下次启动直接进 TabBar
|
|
|
|
|
|
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:KBSexSelectShownKey];
|
|
|
|
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
|
|
|
|
// 切换根控制器到主 TabBar
|
|
|
|
|
|
[weakSelf setupRootVC];
|
|
|
|
|
|
};
|
|
|
|
|
|
self.window.rootViewController = sexVC;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-27 21:11:28 +08:00
|
|
|
|
#pragma mark - Permission presentation
|
|
|
|
|
|
|
2025-10-30 20:23:34 +08:00
|
|
|
|
|
|
|
|
|
|
#pragma mark - Deep Link
|
|
|
|
|
|
|
2025-11-05 18:10:56 +08:00
|
|
|
|
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
|
|
|
|
|
|
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
|
|
|
|
|
|
NSURL *url = userActivity.webpageURL;
|
|
|
|
|
|
if (!url) return NO;
|
|
|
|
|
|
NSString *host = url.host.lowercaseString ?: @"";
|
|
|
|
|
|
if ([host hasSuffix:@"app.tknb.net"]) {
|
|
|
|
|
|
NSString *path = url.path.lowercaseString ?: @"";
|
2025-11-24 19:58:19 +08:00
|
|
|
|
if ([path hasPrefix:@"/ul/settings"]) {
|
|
|
|
|
|
[self kb_openAppSettings];
|
|
|
|
|
|
return YES;
|
|
|
|
|
|
}
|
2026-03-05 14:30:07 +08:00
|
|
|
|
if ([path hasPrefix:@"/ul/recharge"]) {
|
|
|
|
|
|
// 通知扩展:UL 已被主 App 接收
|
|
|
|
|
|
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
|
|
|
|
|
|
(__bridge CFStringRef)KBDarwinULHandled,
|
|
|
|
|
|
NULL, NULL, true);
|
|
|
|
|
|
// 复用 Scheme 处理逻辑,避免重复实现参数解析与跳转
|
|
|
|
|
|
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
|
|
|
|
|
|
NSURLComponents *schemeComponents = [[NSURLComponents alloc] init];
|
|
|
|
|
|
schemeComponents.scheme = @"kbkeyboardAppExtension";
|
|
|
|
|
|
schemeComponents.host = @"recharge";
|
|
|
|
|
|
schemeComponents.queryItems = components.queryItems;
|
|
|
|
|
|
NSURL *schemeURL = schemeComponents.URL;
|
|
|
|
|
|
if (schemeURL) {
|
|
|
|
|
|
[self application:application openURL:schemeURL options:@{}];
|
|
|
|
|
|
}
|
|
|
|
|
|
return YES;
|
|
|
|
|
|
}
|
2025-11-12 21:23:31 +08:00
|
|
|
|
if ([path hasPrefix:@"/ul/login"]) {
|
2025-11-24 19:58:19 +08:00
|
|
|
|
// 区分普通登录与“充值”场景:通过 query 中的 entry=recharge 来判断
|
|
|
|
|
|
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
|
|
|
|
|
|
NSString *entry = nil;
|
|
|
|
|
|
for (NSURLQueryItem *item in components.queryItems ?: @[]) {
|
|
|
|
|
|
if ([item.name isEqualToString:@"entry"]) {
|
|
|
|
|
|
entry = item.value;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-12 21:23:31 +08:00
|
|
|
|
// 通知扩展:UL 已被主 App 接收,扩展可取消回退到 Scheme
|
|
|
|
|
|
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
|
|
|
|
|
|
(__bridge CFStringRef)KBDarwinULHandled,
|
|
|
|
|
|
NULL, NULL, true);
|
2025-11-24 19:58:19 +08:00
|
|
|
|
if ([entry isEqualToString:@"recharge"]) {
|
2026-03-05 14:30:07 +08:00
|
|
|
|
// 充值入口:复用 Scheme 处理逻辑(kbkeyboardAppExtension://recharge?...)
|
|
|
|
|
|
NSURLComponents *schemeComponents = [[NSURLComponents alloc] init];
|
|
|
|
|
|
schemeComponents.scheme = @"kbkeyboardAppExtension";
|
|
|
|
|
|
schemeComponents.host = @"recharge";
|
|
|
|
|
|
schemeComponents.queryItems = components.queryItems;
|
|
|
|
|
|
NSURL *schemeURL = schemeComponents.URL;
|
|
|
|
|
|
if (schemeURL) {
|
|
|
|
|
|
[self application:application openURL:schemeURL options:@{}];
|
|
|
|
|
|
}
|
2025-11-24 19:58:19 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 默认逻辑:登录
|
|
|
|
|
|
[self kb_presentLoginSheetIfNeeded];
|
|
|
|
|
|
}
|
2025-11-12 21:23:31 +08:00
|
|
|
|
return YES;
|
|
|
|
|
|
}
|
2025-11-05 18:10:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return NO;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-30 20:23:34 +08:00
|
|
|
|
// iOS 9+
|
|
|
|
|
|
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
|
|
|
|
|
|
if (!url) return NO;
|
2025-11-05 18:10:56 +08:00
|
|
|
|
// 注意:已对 scheme 做了小写化,比较值也应为小写
|
|
|
|
|
|
if ([[url.scheme lowercaseString] isEqualToString:@"kbkeyboardappextension"]) {
|
2025-10-30 20:46:54 +08:00
|
|
|
|
NSString *urlHost = url.host ?: @"";
|
|
|
|
|
|
NSString *host = [urlHost lowercaseString];
|
|
|
|
|
|
if ([host isEqualToString:@"login"]) { // kbkeyboard://login
|
2025-11-12 21:23:31 +08:00
|
|
|
|
// 兜底路径:Scheme 也发一次“已处理”通知,避免扩展重复尝试
|
|
|
|
|
|
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
|
|
|
|
|
|
(__bridge CFStringRef)KBDarwinULHandled,
|
|
|
|
|
|
NULL, NULL, true);
|
2025-10-30 20:23:34 +08:00
|
|
|
|
[self kb_presentLoginSheetIfNeeded];
|
|
|
|
|
|
return YES;
|
2025-10-30 20:46:54 +08:00
|
|
|
|
} else if ([host isEqualToString:@"settings"]) { // kbkeyboard://settings
|
|
|
|
|
|
[self kb_openAppSettings];
|
|
|
|
|
|
return YES;
|
2026-01-15 19:00:25 +08:00
|
|
|
|
} else if ([host isEqualToString:@"recharge"]) { // kbkeyboard://recharge
|
2025-12-17 16:22:41 +08:00
|
|
|
|
NSDictionary<NSString *, NSString *> *params = [self kb_queryParametersFromURL:url];
|
2026-02-05 14:10:24 +08:00
|
|
|
|
NSString *vipType = [params[@"vipType"] lowercaseString];
|
|
|
|
|
|
BOOL preferSvip = ([vipType isKindOfClass:NSString.class] && vipType.length > 0 &&
|
|
|
|
|
|
([vipType isEqualToString:@"svip"] || [vipType isEqualToString:@"1"]));
|
2025-12-17 16:22:41 +08:00
|
|
|
|
NSString *productId = params[@"productId"];
|
|
|
|
|
|
BOOL autoPay = NO;
|
|
|
|
|
|
NSString *autoFlag = params[@"autoPay"];
|
|
|
|
|
|
if ([autoFlag respondsToSelector:@selector(boolValue)]) {
|
|
|
|
|
|
autoPay = autoFlag.boolValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
NSString *action = params[@"action"].lowercaseString;
|
|
|
|
|
|
if ([action isEqualToString:@"autopay"]) {
|
|
|
|
|
|
autoPay = YES;
|
|
|
|
|
|
}
|
2025-12-17 19:45:39 +08:00
|
|
|
|
|
2026-02-05 14:10:24 +08:00
|
|
|
|
KBPayMainVC *vc = [[KBPayMainVC alloc] init];
|
|
|
|
|
|
vc.initialSelectedIndex = preferSvip ? 1 : 0;
|
|
|
|
|
|
if (!preferSvip) {
|
|
|
|
|
|
BOOL wantsPrefill = NO;
|
|
|
|
|
|
NSString *prefillFlag = params[@"prefill"];
|
|
|
|
|
|
if ([prefillFlag respondsToSelector:@selector(boolValue)] && prefillFlag.boolValue) {
|
|
|
|
|
|
wantsPrefill = YES;
|
|
|
|
|
|
}
|
|
|
|
|
|
NSString *src = params[@"src"];
|
|
|
|
|
|
if ([src isKindOfClass:NSString.class] && [src.lowercaseString isEqualToString:@"keyboard"]) {
|
|
|
|
|
|
wantsPrefill = YES;
|
|
|
|
|
|
}
|
|
|
|
|
|
NSDictionary *prefillPayload = wantsPrefill ? [self kb_consumeSubscriptionPrefillPayloadIfValid] : nil;
|
|
|
|
|
|
if ([prefillPayload isKindOfClass:NSDictionary.class]) {
|
|
|
|
|
|
NSString *payloadProductId = prefillPayload[@"productId"];
|
|
|
|
|
|
if (productId.length == 0 && [payloadProductId isKindOfClass:NSString.class]) {
|
|
|
|
|
|
productId = payloadProductId;
|
|
|
|
|
|
}
|
2025-12-17 19:45:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-05 14:10:24 +08:00
|
|
|
|
if ([prefillPayload isKindOfClass:NSDictionary.class]) {
|
|
|
|
|
|
NSArray *productsJSON = prefillPayload[@"products"];
|
|
|
|
|
|
NSNumber *selectedIndexNumber = prefillPayload[@"selectedIndex"];
|
|
|
|
|
|
NSInteger selectedIndex = [selectedIndexNumber respondsToSelector:@selector(integerValue)] ? selectedIndexNumber.integerValue : NSNotFound;
|
|
|
|
|
|
[vc configureWithProductId:productId
|
|
|
|
|
|
autoPurchase:autoPay
|
|
|
|
|
|
prefillProductsJSON:productsJSON
|
|
|
|
|
|
selectedIndex:selectedIndex];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
[vc configureWithProductId:productId autoPurchase:autoPay];
|
|
|
|
|
|
}
|
2025-12-17 19:45:39 +08:00
|
|
|
|
}
|
2025-11-26 21:16:56 +08:00
|
|
|
|
[KB_CURRENT_NAV pushViewController:vc animated:true];
|
2025-11-21 18:26:02 +08:00
|
|
|
|
return YES;
|
2025-10-30 20:23:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
return NO;
|
|
|
|
|
|
}
|
|
|
|
|
|
return NO;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-17 19:45:39 +08:00
|
|
|
|
- (nullable NSDictionary *)kb_consumeSubscriptionPrefillPayloadIfValid {
|
|
|
|
|
|
NSUserDefaults *ud = [[NSUserDefaults alloc] initWithSuiteName:AppGroup];
|
|
|
|
|
|
if (!ud) { return nil; }
|
|
|
|
|
|
id obj = [ud objectForKey:AppGroup_SubscriptionPrefillPayload];
|
|
|
|
|
|
[ud removeObjectForKey:AppGroup_SubscriptionPrefillPayload];
|
|
|
|
|
|
[ud synchronize];
|
|
|
|
|
|
if (![obj isKindOfClass:NSDictionary.class]) { return nil; }
|
|
|
|
|
|
NSDictionary *payload = (NSDictionary *)obj;
|
|
|
|
|
|
NSNumber *ts = payload[@"ts"];
|
|
|
|
|
|
if (![ts respondsToSelector:@selector(doubleValue)]) { return nil; }
|
|
|
|
|
|
NSTimeInterval age = [NSDate date].timeIntervalSince1970 - ts.doubleValue;
|
|
|
|
|
|
if (age < 0 || age > kKBSubscriptionPrefillTTL) { return nil; }
|
|
|
|
|
|
id products = payload[@"products"];
|
|
|
|
|
|
if (![products isKindOfClass:NSArray.class] || ((NSArray *)products).count == 0) { return nil; }
|
|
|
|
|
|
return payload;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-17 16:22:41 +08:00
|
|
|
|
- (NSDictionary<NSString *, NSString *> *)kb_queryParametersFromURL:(NSURL *)url {
|
|
|
|
|
|
if (!url) { return @{}; }
|
|
|
|
|
|
NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
|
|
|
|
|
|
NSArray<NSURLQueryItem *> *items = components.queryItems ?: @[];
|
|
|
|
|
|
if (items.count == 0) { return @{}; }
|
|
|
|
|
|
NSMutableDictionary<NSString *, NSString *> *dict = [NSMutableDictionary dictionaryWithCapacity:items.count];
|
|
|
|
|
|
for (NSURLQueryItem *item in items) {
|
|
|
|
|
|
if (item.name.length == 0) { continue; }
|
|
|
|
|
|
dict[item.name] = item.value ?: @"";
|
|
|
|
|
|
}
|
|
|
|
|
|
return dict;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-30 20:23:34 +08:00
|
|
|
|
- (void)kb_presentLoginSheetIfNeeded {
|
|
|
|
|
|
// 已登录则不提示
|
2025-11-13 14:11:44 +08:00
|
|
|
|
// BOOL loggedIn = ([AppleSignInManager shared].storedUserIdentifier.length > 0);
|
|
|
|
|
|
// if (loggedIn) return;
|
|
|
|
|
|
// UIViewController *top = [UIViewController kb_topMostViewController];
|
|
|
|
|
|
// if (!top) return;
|
2025-11-12 21:23:31 +08:00
|
|
|
|
// [KBLoginSheetViewController presentIfNeededFrom:top];
|
|
|
|
|
|
[self goLogin];
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)goLogin{
|
2025-12-03 14:30:02 +08:00
|
|
|
|
KBLoginVC *vc = [[KBLoginVC alloc] init];
|
|
|
|
|
|
[KB_CURRENT_NAV pushViewController:vc animated:true];
|
|
|
|
|
|
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
|
|
|
|
|
// KBLoginPopView *view = [[KBLoginPopView alloc] initWithFrame:CGRectMake(0, 0, KB_SCREEN_WIDTH, KB_SCREEN_WIDTH)];
|
|
|
|
|
|
// // 创建并弹出
|
|
|
|
|
|
// LSTPopView *pop = [LSTPopView initWithCustomView:view
|
|
|
|
|
|
// parentView:nil
|
|
|
|
|
|
// popStyle:LSTPopStyleSmoothFromBottom
|
|
|
|
|
|
// dismissStyle:LSTDismissStyleSmoothToBottom];
|
|
|
|
|
|
// pop.hemStyle = LSTHemStyleBottom;
|
|
|
|
|
|
// pop.bgColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
|
|
|
|
|
|
// pop.isClickBgDismiss = YES; // 点击背景关闭
|
|
|
|
|
|
// pop.cornerRadius = 0; // 自定义 view 自处理圆角
|
|
|
|
|
|
//
|
|
|
|
|
|
// __weak typeof(pop) weakPop = pop;
|
|
|
|
|
|
// view.appleLoginHandler = ^{
|
|
|
|
|
|
// [weakPop dismiss];
|
|
|
|
|
|
// // 交给 VM 统一处理 Apple 登录 + 服务端登录
|
|
|
|
|
|
// [[KBLoginVM shared] signInWithAppleFromViewController:KB_CURRENT_NAV completion:^(BOOL success, NSError * _Nullable error) {
|
|
|
|
|
|
// if (success) {
|
|
|
|
|
|
// [KBHUD showInfo:KBLocalized(@"Signed in successfully")];
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// NSString *msg = error.localizedDescription ?: KBLocalized(@"Sign-in failed");
|
|
|
|
|
|
// [KBHUD showInfo:msg];
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }];
|
|
|
|
|
|
// };
|
|
|
|
|
|
// view.closeHandler = ^{ [weakPop dismiss]; };
|
|
|
|
|
|
//
|
|
|
|
|
|
// [pop pop];
|
|
|
|
|
|
// });
|
2025-11-12 21:23:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-10-30 20:23:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-30 20:46:54 +08:00
|
|
|
|
- (void)kb_openAppSettings {
|
|
|
|
|
|
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
|
|
|
|
|
|
UIApplication *app = [UIApplication sharedApplication];
|
|
|
|
|
|
if ([app canOpenURL:url]) {
|
|
|
|
|
|
if (@available(iOS 10.0, *)) {
|
|
|
|
|
|
[app openURL:url options:@{} completionHandler:nil];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
[app openURL:url];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-27 21:11:28 +08:00
|
|
|
|
- (void)kb_presentPermissionIfNeeded
|
|
|
|
|
|
{
|
2025-11-03 16:57:24 +08:00
|
|
|
|
// 该逻辑已迁移到 KBGuideVC,保留占位以兼容旧调用,但不再执行任何操作
|
2025-10-27 21:11:28 +08:00
|
|
|
|
}
|
2025-10-27 18:47:18 +08:00
|
|
|
|
|
2025-11-17 16:16:38 +08:00
|
|
|
|
/// 使用系统网络栈发起一次简单请求(例如拉起蜂窝数据权限弹窗)。
|
|
|
|
|
|
/// 说明:只要真正访问网络,系统在需要时就会弹出“是否允许使用蜂窝数据”等提示。
|
|
|
|
|
|
- (void)kb_fireStartupNetworkRequest {
|
2025-11-17 18:51:06 +08:00
|
|
|
|
// [[KBNetworkManager shared] GET:@"https://www.apple.com" parameters:nil headers:nil completion:^(id _Nullable jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
|
|
|
//
|
|
|
|
|
|
// }];
|
2025-11-17 16:16:38 +08:00
|
|
|
|
NSURL *url = [NSURL URLWithString:@"https://www.apple.com"];
|
|
|
|
|
|
if (!url) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
|
|
|
|
|
|
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
|
|
|
|
|
|
NSURLSessionDataTask *task = [session dataTaskWithURL:url
|
|
|
|
|
|
completionHandler:^(NSData * _Nullable data,
|
|
|
|
|
|
NSURLResponse * _Nullable response,
|
|
|
|
|
|
NSError * _Nullable error) {
|
|
|
|
|
|
// 回到主线程弹出一个简单提示,证明网络请求已经发起
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
|
});
|
|
|
|
|
|
}];
|
|
|
|
|
|
[task resume];
|
|
|
|
|
|
}
|
2025-10-27 21:55:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-(void)getNetJudge {
|
|
|
|
|
|
AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
|
|
|
|
|
|
[netManager startMonitoring];
|
2025-11-21 18:36:00 +08:00
|
|
|
|
__weak typeof(self) weakSelf = self;
|
2025-10-27 21:55:05 +08:00
|
|
|
|
[netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
|
|
|
|
|
|
if (status == AFNetworkReachabilityStatusReachableViaWiFi){
|
|
|
|
|
|
// [PublicObj saveNetReachability:@"wifi"];
|
|
|
|
|
|
}else if (status == AFNetworkReachabilityStatusReachableViaWWAN){
|
|
|
|
|
|
// [PublicObj saveNetReachability:@"wwan"];
|
|
|
|
|
|
}else{
|
|
|
|
|
|
// [PublicObj saveNetReachability:@"unknown"];
|
|
|
|
|
|
}
|
2025-11-21 18:36:00 +08:00
|
|
|
|
|
|
|
|
|
|
// 网络状态变化时检查一次,并在无网络时弹出“去设置 / 取消”提示框
|
|
|
|
|
|
__strong typeof(weakSelf) strongSelf = weakSelf;
|
|
|
|
|
|
if (!strongSelf) { return; }
|
|
|
|
|
|
[strongSelf kb_checkNetworkAndShowAlertIfNeeded];
|
2025-10-27 21:55:05 +08:00
|
|
|
|
}];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-27 22:02:46 +08:00
|
|
|
|
|
2025-11-21 18:26:02 +08:00
|
|
|
|
//static BOOL KBIsKeyboardEnabled(void) {
|
|
|
|
|
|
// for (UITextInputMode *mode in [UITextInputMode activeInputModes]) {
|
|
|
|
|
|
// NSString *identifier = nil;
|
|
|
|
|
|
// @try {
|
|
|
|
|
|
// identifier = [mode valueForKey:@"identifier"]; // not a public API
|
|
|
|
|
|
// } @catch (__unused NSException *e) {
|
|
|
|
|
|
// identifier = nil;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// if ([identifier isKindOfClass:[NSString class]] &&
|
|
|
|
|
|
// [identifier rangeOfString:kKBKeyboardExtensionBundleId].location != NSNotFound) {
|
|
|
|
|
|
// return YES;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return NO;
|
|
|
|
|
|
//}
|
2025-10-27 22:02:46 +08:00
|
|
|
|
|
2025-11-17 18:51:06 +08:00
|
|
|
|
#pragma mark - Network check & alert
|
|
|
|
|
|
|
|
|
|
|
|
- (void)kb_checkNetworkAndShowAlertIfNeeded {
|
2025-11-21 20:59:39 +08:00
|
|
|
|
// 首次安装且尚未完成首页引导时,不弹“无网络”提示,避免在系统蜂窝数据权限弹窗前就打扰用户
|
|
|
|
|
|
static NSString *const kKBHasLaunchedOnce = @"KBHasLaunchedOnce";
|
|
|
|
|
|
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
|
if (![ud boolForKey:kKBHasLaunchedOnce]) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 仅在应用处于前台时提示
|
|
|
|
|
|
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-17 18:51:06 +08:00
|
|
|
|
AFNetworkReachabilityStatus status = [AFNetworkReachabilityManager sharedManager].networkReachabilityStatus;
|
|
|
|
|
|
|
|
|
|
|
|
// 只有在“明确不可达”时才弹
|
|
|
|
|
|
if (status == AFNetworkReachabilityStatusNotReachable ||
|
|
|
|
|
|
status == AFNetworkReachabilityStatusUnknown) {
|
|
|
|
|
|
[self kb_showNoNetworkAlert];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)kb_showNoNetworkAlert {
|
|
|
|
|
|
UIViewController *rootVC = self.window.rootViewController;
|
|
|
|
|
|
if (!rootVC) return;
|
|
|
|
|
|
|
|
|
|
|
|
// 防止重复弹(例如已经有一个 alert 在上面了)
|
|
|
|
|
|
if ([rootVC.presentedViewController isKindOfClass:[UIAlertController class]]) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-17 20:07:39 +08:00
|
|
|
|
UIAlertController *alert = [UIAlertController alertControllerWithTitle:KBLocalized(@"Network unavailable")
|
|
|
|
|
|
message:KBLocalized(@"Please check this app's wireless-data permission or network connection in Settings.")
|
2025-11-17 18:51:06 +08:00
|
|
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
|
|
|
|
|
2025-11-17 20:07:39 +08:00
|
|
|
|
UIAlertAction *settings = [UIAlertAction actionWithTitle:KBLocalized(@"Open Settings")
|
2025-11-17 18:51:06 +08:00
|
|
|
|
style:UIAlertActionStyleDefault
|
|
|
|
|
|
handler:^(UIAlertAction * _Nonnull action) {
|
|
|
|
|
|
[self kb_openAppSettings]; // 你已经实现好的跳设置方法
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
2025-11-17 20:07:39 +08:00
|
|
|
|
UIAlertAction *cancel = [UIAlertAction actionWithTitle:KBLocalized(@"Cancel")
|
2025-11-17 18:51:06 +08:00
|
|
|
|
style:UIAlertActionStyleCancel
|
|
|
|
|
|
handler:nil];
|
|
|
|
|
|
|
|
|
|
|
|
[alert addAction:settings];
|
|
|
|
|
|
[alert addAction:cancel];
|
|
|
|
|
|
|
|
|
|
|
|
[rootVC presentViewController:alert animated:YES completion:nil];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-02 20:52:02 +08:00
|
|
|
|
- (void)kb_installDefaultSkinIfNeeded {
|
2026-03-05 17:42:50 +08:00
|
|
|
|
NSLog(@"[AppDelegate] Installing/applying default skin if needed...");
|
|
|
|
|
|
NSUserDefaults *shared = [[NSUserDefaults alloc] initWithSuiteName:AppGroup];
|
|
|
|
|
|
NSString *languageCode = [shared stringForKey:AppGroup_SelectedKeyboardLanguageCode];
|
|
|
|
|
|
if (languageCode.length == 0) {
|
|
|
|
|
|
NSString *appLanguageCode = [KBLocalizationManager shared].currentLanguageCode ?: KBLanguageCodeEnglish;
|
|
|
|
|
|
languageCode = [self kb_defaultKeyboardLanguageCodeForAppLanguageCode:appLanguageCode];
|
2026-03-02 20:52:02 +08:00
|
|
|
|
}
|
2026-03-05 17:42:50 +08:00
|
|
|
|
[self kb_applyDefaultSkinForKeyboardLanguageIfNeeded:languageCode ?: @"en"];
|
2026-03-02 20:52:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-27 18:47:18 +08:00
|
|
|
|
@end
|