176 lines
5.8 KiB
Objective-C
176 lines
5.8 KiB
Objective-C
//
|
|
// AppDelegate.m
|
|
// keyBoard
|
|
//
|
|
// Created by 张伟 on 2025/10/27.
|
|
//
|
|
|
|
#import "AppDelegate.h"
|
|
#import "KBPermissionViewController.h"
|
|
#import <AFNetworking/AFNetworking.h>
|
|
#import <Bugly/Bugly.h>
|
|
#import "BaseTabBarController.h"
|
|
#import "LoginViewController.h"
|
|
#import "KBLoginSheetViewController.h"
|
|
#import "AppleSignInManager.h"
|
|
#import <objc/message.h>
|
|
|
|
// 注意:用于判断系统已启用本输入法扩展的 bundle id 需与扩展 target 的
|
|
// PRODUCT_BUNDLE_IDENTIFIER 完全一致。
|
|
// 当前工程的 CustomKeyboard target 为 com.keyBoardst.CustomKeyboard
|
|
static NSString * const kKBKeyboardExtensionBundleId = @"com.keyBoardst.CustomKeyboard";
|
|
|
|
@implementation AppDelegate
|
|
|
|
|
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
|
|
|
|
[self setupRootVC];
|
|
// 主工程默认开启网络总开关(键盘扩展仍需用户允许完全访问后再行开启)
|
|
[KBNetworkManager shared].enabled = YES;
|
|
/// 获取网络权限
|
|
[self getNetJudge];
|
|
/// Bugly
|
|
BuglyConfig *buglyConfig = [BuglyConfig new];
|
|
/// 设置GroupID进行配置
|
|
// buglyConfig.applicationGroupIdentifier = @"";
|
|
[Bugly startWithAppId:BuglyId config:buglyConfig];
|
|
/// 判断获取键盘权限(统一管理):
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
UIViewController *top = [self kb_topMostViewController];
|
|
if (top) {
|
|
Class mgrCls = NSClassFromString(@"KBKeyboardPermissionManager");
|
|
if (mgrCls && [mgrCls respondsToSelector:@selector(shared)]) {
|
|
id mgr = [mgrCls performSelector:@selector(shared)];
|
|
if ([mgr respondsToSelector:@selector(presentPermissionIfNeededFrom:)]) {
|
|
// 避免私有 API 静态链接,运行时调用
|
|
void (*func)(id, SEL, UIViewController *) = (void (*)(id, SEL, UIViewController *))objc_msgSend;
|
|
func(mgr, @selector(presentPermissionIfNeededFrom:), top);
|
|
}
|
|
} else {
|
|
// 兜底走原有逻辑(仅判断是否启用键盘)
|
|
[self kb_presentPermissionIfNeeded];
|
|
}
|
|
}
|
|
});
|
|
return YES;
|
|
}
|
|
|
|
|
|
- (void)setupRootVC{
|
|
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
|
|
self.window.backgroundColor = [UIColor whiteColor];
|
|
[self.window makeKeyAndVisible];
|
|
BaseTabBarController *vc = [[BaseTabBarController alloc] init];
|
|
self.window.rootViewController = vc;
|
|
}
|
|
|
|
#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
|
|
|
|
// iOS 9+
|
|
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
|
|
if (!url) return NO;
|
|
if ([[url.scheme lowercaseString] isEqualToString:@"kbkeyboard"]) {
|
|
NSString *urlHost = url.host ?: @"";
|
|
NSString *host = [urlHost lowercaseString];
|
|
if ([host isEqualToString:@"login"]) { // kbkeyboard://login
|
|
[self kb_presentLoginSheetIfNeeded];
|
|
return YES;
|
|
} else if ([host isEqualToString:@"settings"]) { // kbkeyboard://settings
|
|
[self kb_openAppSettings];
|
|
return YES;
|
|
}
|
|
return NO;
|
|
}
|
|
return NO;
|
|
}
|
|
|
|
- (void)kb_presentLoginSheetIfNeeded {
|
|
// 已登录则不提示
|
|
BOOL loggedIn = ([AppleSignInManager shared].storedUserIdentifier.length > 0);
|
|
if (loggedIn) return;
|
|
UIViewController *top = [self kb_topMostViewController];
|
|
if (!top) return;
|
|
[KBLoginSheetViewController presentIfNeededFrom:top];
|
|
}
|
|
|
|
- (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];
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)kb_presentPermissionIfNeeded
|
|
{
|
|
BOOL enabled = KBIsKeyboardEnabled();
|
|
UIViewController *top = [self kb_topMostViewController];
|
|
if (!top) return;
|
|
if ([top isKindOfClass:[KBPermissionViewController class]]) {
|
|
if (enabled) {
|
|
[top dismissViewControllerAnimated:YES completion:nil];
|
|
}
|
|
return;
|
|
}
|
|
if (!enabled) {
|
|
KBPermissionViewController *guide = [KBPermissionViewController new];
|
|
guide.modalPresentationStyle = UIModalPresentationFullScreen;
|
|
[top presentViewController:guide animated:YES completion:nil];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-(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"];
|
|
}
|
|
}];
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
@end
|