安全键盘

产生原因:

1)避免第三方读取系统键盘缓存

2)防止屏幕录制(自己定制的键盘按键不加按下效果)

思路:

1)首先捕获系统键盘的弹出,收回通知

2)创建一个更高级别的窗口挡住系统键盘

3)需要抛出一个idtextInput的弱引用切换焦点

键盘安全模型:

@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

你可能感兴趣的:(安全键盘)