获取extension,判断第一次

This commit is contained in:
2025-10-27 21:11:28 +08:00
parent a9625369f7
commit d4ebfb9909
6 changed files with 92 additions and 42 deletions

View File

@@ -9,12 +9,13 @@
@implementation KBToolBar
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
}
return self;
}
*/
@end

View File

@@ -38,15 +38,53 @@ static BOOL KBIsKeyboardEnabled(void) {
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
// Present guide if the custom keyboard is not enabled yet
// [[NSNotificationCenter defaultCenter] addObserver:self
// selector:@selector(applicationDidBecomeActiveNotification:)
// name:UIApplicationDidBecomeActiveNotification
// object:nil];
dispatch_async(dispatch_get_main_queue(), ^{
if (!KBIsKeyboardEnabled()) {
KBPermissionViewController *guide = [KBPermissionViewController new];
guide.modalPresentationStyle = UIModalPresentationFullScreen;
[self.window.rootViewController presentViewController:guide animated:YES completion:nil];
}
[self kb_presentPermissionIfNeeded];
});
return YES;
}
//
//#pragma mark - Active notifications
//
//- (void)applicationDidBecomeActiveNotification:(NSNotification *)note
//{
// [self kb_presentPermissionIfNeeded];
//}
#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;
}
- (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];
}
}
@end

View File

@@ -1,5 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Title</key>
<string>Keyboard</string>
<key>Type</key>
<string>PSGroupSpecifier</string>
</dict>
</array>
<key>StringsTable</key>
<string>Root</string>
</dict>
</plist>

View File

@@ -18,7 +18,6 @@
[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];
@@ -28,7 +27,6 @@
[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];
@@ -38,7 +36,6 @@
title.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:title];
// Step tips
UILabel *tips = [[UILabel alloc] init];
tips.text = @"1 开启键盘 > 2 允许完全访问";
tips.font = [UIFont systemFontOfSize:14];
@@ -48,7 +45,6 @@
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;
@@ -57,7 +53,6 @@
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];
@@ -70,7 +65,6 @@
[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];
@@ -87,20 +81,7 @@
}
- (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, *)) {
@@ -109,7 +90,6 @@
[app openURL:url];
}
}
//#endif
}
@end

View File

@@ -8,22 +8,29 @@
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)UITextField *textField;
@property (nonatomic, strong) UITextView *textView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
self.textField = [[UITextField alloc] init];
self.textField.frame = CGRectMake(100, 100, 200, 40);
self.textField.layer.borderWidth = 1;
self.textField.layer.borderColor = [UIColor redColor].CGColor;
[self.view addSubview:self.textField];
[self setupTextField];
}
- (void)setupTextField {
CGRect frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 200)/2, ([UIScreen mainScreen].bounds.size.height - 200)/2, 200, 200);
self.textView = [[UITextView alloc] initWithFrame:frame];
self.textView.text = @"测试";
self.textView.layer.borderColor = [UIColor blackColor].CGColor;
self.textView.layer.borderWidth = 0.5;
[self.view addSubview:self.textView];
[self.textView becomeFirstResponder];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.textView resignFirstResponder];
}