Files
keyboard/keyBoard/AppDelegate.m

257 lines
9.9 KiB
Mathematica
Raw Normal View History

2025-10-27 18:47:18 +08:00
//
// AppDelegate.m
// keyBoard
//
// Created by on 2025/10/27.
//
#import "AppDelegate.h"
2025-10-27 20:07:37 +08:00
#import "KBPermissionViewController.h"
2025-10-27 21:55:05 +08:00
#import <AFNetworking/AFNetworking.h>
#if !DEBUG
2025-10-27 21:55:05 +08:00
#import <Bugly/Bugly.h>
#endif
2025-10-29 14:17:26 +08:00
#import "BaseTabBarController.h"
2025-10-30 14:29:11 +08:00
#import "LoginViewController.h"
2025-10-30 20:23:34 +08:00
#import "KBLoginSheetViewController.h"
#import "AppleSignInManager.h"
2025-11-03 13:25:41 +08:00
#import <objc/message.h>
2025-11-12 21:23:31 +08:00
#import "KBULBridge.h" // Darwin UL
#import "LSTPopView.h"
#import "KBLoginPopView.h"
2025-11-14 16:34:01 +08:00
#import "IAPVerifyTransactionObj.h"
#import "FGIAPManager.h"
2025-11-17 16:16:38 +08:00
#import "KBSexSelVC.h"
2025-10-30 18:31:12 +08:00
// bundle id target
// PRODUCT_BUNDLE_IDENTIFIER
// CustomKeyboard target com.loveKey.nyx.CustomKeyboard
static NSString * const kKBKeyboardExtensionBundleId = @"com.loveKey.nyx.CustomKeyboard";
2025-10-27 18:47:18 +08:00
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2025-11-14 16:34:01 +08:00
// 1.
[[FGIAPManager shared] setConfigureWith:[IAPVerifyTransactionObj new]];
2025-11-17 16:16:38 +08:00
// Skip TabBar
BOOL hasShownSexVC = [[NSUserDefaults standardUserDefaults] boolForKey:KBSexSelectShownKey];
2025-11-17 16:42:32 +08:00
// if (hasShownSexVC) {
// [self setupRootVC];
// } else {
2025-11-17 16:16:38 +08:00
[self setupSexSelectRootVC];
2025-11-17 16:42:32 +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];
2025-11-17 16:16:38 +08:00
///
[self kb_fireStartupNetworkRequest];
#if !DEBUG
/// Bugly
2025-10-27 22:02:46 +08:00
BuglyConfig *buglyConfig = [BuglyConfig new];
/// GroupID
// buglyConfig.applicationGroupIdentifier = @"";
[Bugly startWithAppId:BuglyId config:buglyConfig];
#endif
2025-11-03 16:57:24 +08:00
// KBGuideVC
2025-10-27 18:47:18 +08:00
return YES;
}
2025-10-27 21:11:28 +08:00
2025-11-03 16:57:24 +08:00
- (void)applicationDidBecomeActive:(UIApplication *)application{}
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-10-30 20:23:34 +08:00
BaseTabBarController *vc = [[BaseTabBarController alloc] init];
2025-10-29 14:17:26 +08:00
self.window.rootViewController = vc;
}
2025-11-17 16:16:38 +08:00
///
- (void)setupSexSelectRootVC {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
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
- (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 ?: @"";
if ([path hasPrefix:@"/ul/settings"]) { [self kb_openAppSettings]; return YES; }
2025-11-12 21:23:31 +08:00
if ([path hasPrefix:@"/ul/login"]) {
// UL App 退 Scheme
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
(__bridge CFStringRef)KBDarwinULHandled,
NULL, NULL, true);
[self kb_presentLoginSheetIfNeeded];
return YES;
}
}
}
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;
// 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;
2025-10-30 20:23:34 +08:00
}
return NO;
}
return NO;
}
- (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{
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];
2025-11-13 19:07:59 +08:00
// VM Apple +
[[KBLoginVM shared] signInWithAppleFromViewController:KB_CURRENT_NAV completion:^(BOOL success, NSError * _Nullable error) {
if (success) {
[KBHUD showInfo:@"登录成功"];
} else {
NSString *msg = error.localizedDescription ?: @"登录失败";
[KBHUD showInfo:msg];
}
2025-11-12 21:23:31 +08:00
}];
};
view.closeHandler = ^{ [weakPop dismiss]; };
[pop pop];
});
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 {
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];
[netManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
if (status == AFNetworkReachabilityStatusReachableViaWiFi){
// [PublicObj saveNetReachability:@"wifi"];
}else if (status == AFNetworkReachabilityStatusReachableViaWWAN){
// [PublicObj saveNetReachability:@"wwan"];
}else{
// [PublicObj saveNetReachability:@"unknown"];
}
}];
}
2025-10-27 22:02:46 +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 18:47:18 +08:00
@end