This commit is contained in:
2025-11-14 16:34:01 +08:00
parent eacac8425c
commit 66a1ddef66
16 changed files with 262 additions and 55 deletions

View File

@@ -29,6 +29,8 @@ typedef NS_ENUM(NSInteger, KBGuideItemType) {
@property (nonatomic, strong) NSMutableArray<NSDictionary *> *items; // [{type, text}]
///
@property (nonatomic, strong, nullable) KBPermissionViewController *permVC;
///
@property (nonatomic, copy, nullable) NSString *kb_lastInputModeIdentifier;
@end
@@ -45,8 +47,9 @@ typedef NS_ENUM(NSInteger, KBGuideItemType) {
[self.inputBar addSubview:self.textField];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self.view);
make.left.right.equalTo(self.view);
make.bottom.equalTo(self.view);
make.top.mas_equalTo(KB_NAV_TOTAL_HEIGHT);
}];
[self.inputBar mas_makeConstraints:^(MASConstraintMaker *make) {
@@ -83,8 +86,17 @@ typedef NS_ENUM(NSInteger, KBGuideItemType) {
// /
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kb_checkKeyboardPermission) name:UIApplicationDidBecomeActiveNotification object:nil];
// 使
NSArray<NSString *> *modeNotiNames = @[ @"UITextInputCurrentInputModeDidChangeNotification",
@"UITextInputCurrentInputModeDidChange" ];
for (NSString *n in modeNotiNames) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kb_inputModeDidChange:) name:n object:nil];
}
//
[self kb_preparePermissionOverlayIfNeeded];
}
- (void)dealloc {
@@ -95,6 +107,19 @@ typedef NS_ENUM(NSInteger, KBGuideItemType) {
[super viewWillAppear:animated];
//
[self kb_checkKeyboardPermission];
// /
BOOL permissionReady = (self.permVC && self.permVC.view.hidden == YES);
if (permissionReady) {
// textInputMode
if (![self.textField isFirstResponder]) {
[self.textField becomeFirstResponder];
}
// 线
// UIKeyboardWillChangeFrame
dispatch_async(dispatch_get_main_queue(), ^{
[self kb_evaluateCurrentInputModeAndNotifyIfNeeded];
});
}
}
///
@@ -179,6 +204,8 @@ typedef NS_ENUM(NSInteger, KBGuideItemType) {
// self.tableView.scrollIndicatorInsets = inset;
} completion:^(BOOL finished) {
[self scrollToBottomAnimated:YES];
//
[self kb_evaluateCurrentInputModeAndNotifyIfNeeded];
}];
}
@@ -191,6 +218,58 @@ typedef NS_ENUM(NSInteger, KBGuideItemType) {
}
}
#pragma mark - Keyboard Detection
///
///
/// - `UITextField.textInputMode`
/// - KVC `identifier` bundle id
/// - `textField`
- (BOOL)kb_isMyExtensionKeyboardSelected {
UITextInputMode *mode = self.textField.textInputMode;
if (!mode) { return NO; }
NSString *identifier = nil;
@try {
// App
identifier = [mode valueForKey:@"identifier"];
} @catch (__unused NSException *e) {
identifier = nil;
}
if (![identifier isKindOfClass:[NSString class]]) { return NO; }
return [identifier rangeOfString:KB_KEYBOARD_EXTENSION_BUNDLE_ID].location != NSNotFound;
}
- (void)kb_inputModeDidChange:(NSNotification *)note {
//
[self kb_evaluateCurrentInputModeAndNotifyIfNeeded];
}
/// identifier nil
- (NSString *)kb_currentInputModeIdentifier {
UITextInputMode *mode = self.textField.textInputMode;
if (!mode) return nil;
NSString *identifier = nil;
@try { identifier = [mode valueForKey:@"identifier"]; } @catch (__unused NSException *e) { identifier = nil; }
return [identifier isKindOfClass:NSString.class] ? identifier : nil;
}
///
- (void)kb_evaluateCurrentInputModeAndNotifyIfNeeded {
//
if (![self.textField isFirstResponder]) return;
//
if (self.permVC && self.permVC.view.hidden == NO) return;
NSString *currId = [self kb_currentInputModeIdentifier];
if (currId.length == 0) return;
if ([self.kb_lastInputModeIdentifier isEqualToString:currId]) return; //
self.kb_lastInputModeIdentifier = currId;
BOOL isMine = [currId rangeOfString:KB_KEYBOARD_EXTENSION_BUNDLE_ID].location != NSNotFound;
[KBHUD showInfo:(isMine ? @"是自己的键盘" : @"❎不是自己的键盘")];
}
#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {