From d4ebfb99090f6fada7b84785632b4e6f00959dc8 Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Mon, 27 Oct 2025 21:11:28 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96extension=EF=BC=8C=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E7=AC=AC=E4=B8=80=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CustomKeyboard/View/KBToolBar.m | 13 +++--- keyBoard/AppDelegate.m | 50 +++++++++++++++++++++--- keyBoard/Info.plist | 8 +++- keyBoard/Settings.bundle/Root.plist | 18 +++++++++ keyBoard/VC/KBPermissionViewController.m | 20 ---------- keyBoard/ViewController.m | 25 +++++++----- 6 files changed, 92 insertions(+), 42 deletions(-) create mode 100644 keyBoard/Settings.bundle/Root.plist diff --git a/CustomKeyboard/View/KBToolBar.m b/CustomKeyboard/View/KBToolBar.m index db12de3..dbea27a 100644 --- a/CustomKeyboard/View/KBToolBar.m +++ b/CustomKeyboard/View/KBToolBar.m @@ -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 diff --git a/keyBoard/AppDelegate.m b/keyBoard/AppDelegate.m index 72c1d94..13d92e0 100644 --- a/keyBoard/AppDelegate.m +++ b/keyBoard/AppDelegate.m @@ -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 diff --git a/keyBoard/Info.plist b/keyBoard/Info.plist index 0c67376..6a6654d 100644 --- a/keyBoard/Info.plist +++ b/keyBoard/Info.plist @@ -1,5 +1,11 @@ - + + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + + diff --git a/keyBoard/Settings.bundle/Root.plist b/keyBoard/Settings.bundle/Root.plist new file mode 100644 index 0000000..ab6e6c3 --- /dev/null +++ b/keyBoard/Settings.bundle/Root.plist @@ -0,0 +1,18 @@ + + + + + PreferenceSpecifiers + + + Title + Keyboard + Type + PSGroupSpecifier + + + StringsTable + Root + + + diff --git a/keyBoard/VC/KBPermissionViewController.m b/keyBoard/VC/KBPermissionViewController.m index 275bd19..83692c8 100644 --- a/keyBoard/VC/KBPermissionViewController.m +++ b/keyBoard/VC/KBPermissionViewController.m @@ -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 diff --git a/keyBoard/ViewController.m b/keyBoard/ViewController.m index 531aba0..46187c3 100644 --- a/keyBoard/ViewController.m +++ b/keyBoard/ViewController.m @@ -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 *)touches withEvent:(UIEvent *)event { + [self.textView resignFirstResponder]; }