This commit is contained in:
2025-10-27 20:07:37 +08:00
parent 5e44ac155b
commit a9625369f7
5 changed files with 176 additions and 7 deletions

View File

@@ -7,10 +7,25 @@
#import "AppDelegate.h"
#import "ViewController.h"
#import "KBPermissionViewController.h"
@interface AppDelegate ()
static NSString * const kKBKeyboardExtensionBundleId = @"com.keyBoard.CustomKeyboard";
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
@implementation AppDelegate
@@ -22,6 +37,15 @@
[self.window makeKeyAndVisible];
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
// Present guide if the custom keyboard is not enabled yet
dispatch_async(dispatch_get_main_queue(), ^{
if (!KBIsKeyboardEnabled()) {
KBPermissionViewController *guide = [KBPermissionViewController new];
guide.modalPresentationStyle = UIModalPresentationFullScreen;
[self.window.rootViewController presentViewController:guide animated:YES completion:nil];
}
});
return YES;
}

View File

@@ -0,0 +1,16 @@
//
// KBPermissionViewController.h
// CustomKeyboard
//
// Created by Mac on 2025/10/27.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface KBPermissionViewController : UIViewController
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,115 @@
//
// KBPermissionViewController.m
// CustomKeyboard
//
// Created by Mac on 2025/10/27.
//
#import "KBPermissionViewController.h"
#import <TargetConditionals.h>
@interface KBPermissionViewController ()
@end
@implementation KBPermissionViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.96 alpha:1.0];
// Close button (top-right)
UIButton *close = [UIButton buttonWithType:UIButtonTypeSystem];
[close setTitle:@"X" forState:UIControlStateNormal];
close.titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightSemibold];
close.tintColor = [UIColor darkTextColor];
close.frame = CGRectMake(self.view.bounds.size.width - 44, 44, 28, 28);
close.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
[close addTarget:self action:@selector(dismissSelf) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:close];
// Title
UILabel *title = [[UILabel alloc] init];
title.text = @"启用输入法";
title.font = [UIFont systemFontOfSize:22 weight:UIFontWeightSemibold];
title.textColor = [UIColor blackColor];
title.textAlignment = NSTextAlignmentCenter;
title.frame = CGRectMake(24, 100, self.view.bounds.size.width - 48, 28);
title.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:title];
// Step tips
UILabel *tips = [[UILabel alloc] init];
tips.text = @"1 开启键盘 > 2 允许完全访问";
tips.font = [UIFont systemFontOfSize:14];
tips.textColor = [UIColor darkGrayColor];
tips.textAlignment = NSTextAlignmentCenter;
tips.frame = CGRectMake(24, CGRectGetMaxY(title.frame) + 8, self.view.bounds.size.width - 48, 20);
tips.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:tips];
// Illustration placeholder (simple rounded rect)
UIView *card = [[UIView alloc] initWithFrame:CGRectMake(32, CGRectGetMaxY(tips.frame) + 28, self.view.bounds.size.width - 64, 260)];
card.backgroundColor = [UIColor whiteColor];
card.layer.cornerRadius = 16;
card.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.1].CGColor;
card.layer.shadowOpacity = 1;
card.layer.shadowRadius = 12;
[self.view addSubview:card];
// Button to open Settings
UIButton *open = [UIButton buttonWithType:UIButtonTypeSystem];
[open setTitle:@"去设置中开启" forState:UIControlStateNormal];
open.titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightSemibold];
[open setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
open.backgroundColor = [UIColor colorWithRed:0.22 green:0.49 blue:0.96 alpha:1.0];
open.layer.cornerRadius = 8;
CGFloat btnW = self.view.bounds.size.width - 64;
open.frame = CGRectMake(32, CGRectGetMaxY(card.frame) + 32, btnW, 48);
open.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[open addTarget:self action:@selector(openSettings) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:open];
// Bottom helper text
UILabel *help = [[UILabel alloc] init];
help.text = @"没有找到键盘? 请前往 设置 > 通用 > 键盘 > 键盘 > 添加新键盘";
help.font = [UIFont systemFontOfSize:12];
help.textColor = [UIColor grayColor];
help.textAlignment = NSTextAlignmentCenter;
help.numberOfLines = 2;
help.frame = CGRectMake(24, CGRectGetMaxY(open.frame) + 12, self.view.bounds.size.width - 48, 36);
help.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:help];
}
- (void)dismissSelf {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)openSettings {
// In app extensions you cannot call UIApplication. Use extension-safe code.
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
//#if TARGET_OS_APP_EXTENSION
// // Try to open Settings via the extension context (extension-safe). This may
// // fail depending on the extension type and system policy.
// if (self.extensionContext) {
// if (@available(iOS 10.0, *)) {
// [self.extensionContext openURL:url completionHandler:nil];
// } else {
// [self.extensionContext openURL:url completionHandler:nil];
// }
// }
//#else
UIApplication *app = [UIApplication sharedApplication];
if ([app canOpenURL:url]) {
if (@available(iOS 10.0, *)) {
[app openURL:url options:@{} completionHandler:nil];
} else {
[app openURL:url];
}
}
//#endif
}
@end