安全鍵盤

產(chǎn)生原因:

1)避免第三方讀取系統(tǒng)鍵盤緩存

2)防止屏幕錄制(自己定制的鍵盤按鍵不加按下效果)

思路:

1)首先捕獲系統(tǒng)鍵盤的彈出,收回通知

2)創(chuàng)建一個(gè)更高級(jí)別的窗口擋住系統(tǒng)鍵盤

3)需要拋出一個(gè)idtextInput的弱引用切換焦點(diǎn)

鍵盤安全模型:

@interface WQSafeKeyboard : UIWindow@property (nonatomic, weak, setter = focusOnTextFiled:) UITextField *textFiled;+ (WQSafeKeyboard *)deploySafeKeyboard;@end@interface WQSafeKeyboard()@property (nonatomic, strong)WQInterKeyboard *keyboard;@end@implementation WQSafeKeyboard+ (WQSafeKeyboard *)deploySafeKeyboard{WQSafeKeyboard *kb = [[WQSafeKeyboard alloc]init];[kb addObserver];return kb;}- (instancetype)init{if (self = [super init]) {self.windowLevel = UIWindowLevelAlert;self.frame = CGRectZero;self.rootViewController = self.keyboard;}return self;}- (void)dealloc{[[NSNotificationCenter defaultCenter] removeObserver:self];}- (WQInterKeyboard *)keyboard{if (!_keyboard) {_keyboard = [[WQInterKeyboard alloc]init];}return _keyboard;}- (void)focusOnTextFiled:(UITextField *)textFiled{_textFiled = textFiled;self.keyboard.textField = _textFiled;}- (void)addObserver{[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];}- (void)keyboardWillShow:(NSNotification *)notification{if (![self.textFiled isFirstResponder]) {return;}[self keyboardAnimationWithNotification:notification];}- (void)keyboardWillHide:(NSNotification *)notification{if (![self.textFiled isFirstResponder]) {return;}[self keyboardAnimationWithNotification:notification];}- (void)keyboardAnimationWithNotification:(NSNotification *)notification{[self makeKeyAndVisible];NSDictionary *userInfo = [notification userInfo];CGRect kbFrame_end,kbFrame_begin;NSTimeInterval animationDuration;UIViewAnimationCurve animationCurve;[userInfo[UIKeyboardFrameEndUserInfoKey] getValue:&kbFrame_end];[userInfo[UIKeyboardFrameBeginUserInfoKey] getValue:&kbFrame_begin];[userInfo[UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];[userInfo[UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];self.frame = [self resizeFrameToAdjust:kbFrame_begin];[UIView animateWithDuration:animationDurationdelay:0options:(animationCurve<<16)animations:^{self.frame = [self resizeFrameToAdjust:kbFrame_end];}completion:^(BOOL finished) {}];if ([notification.name isEqualToString:UIKeyboardWillHideNotification]) {[self resignKeyWindow];}}- (CGRect)resizeFrameToAdjust:(CGRect)frame{if ([[UIApplication sharedApplication] isStatusBarHidden] )return frame;if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {frame = CGRectMake(frame.origin.x,frame.origin.y - STATUSBAR_HEIGHT,frame.size.width,frame.size.height);}return frame;}@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容