键盘缓存与安全键盘

来源:互联网 发布:男生基本款帽子 知乎 编辑:程序博客网 时间:2024/05/22 11:40

键盘缓存与安全键盘



大部分中文应用弹出的默认键盘是简体中文输入法键盘,在输入用户名和密码的时候,如果使用简体中文输入法键盘,输入英文字符和数字字符的用户名和密码时,会自动启动系统输入法自动更正提示,然后用户的输入记录会被缓存下来。

系统键盘缓存最方便拿到的就是利用系统输入法自动更正的字符串输入记录。
缓存文件的地址是:/private/var/mobile/Library/Keyboard/dynamic-text.dat

导出该缓存文件,查看内容,欣喜的发现一切输入记录都是明文存储的。因为系统不会把所有的用户输入记录都当作密码等敏感信息来处理。

一般情况下,一个常规iPhone用户的dynamic-text.dat文件,高频率出现的字符串就是用户名和密码。


所以,一般银行客户端app输入密码时都不使用系统键盘,而使用自己定制的键盘,原因主要有2个:
1)避免第三方读取系统键盘缓存
2)防止屏幕录制 (自己定制的键盘按键不加按下效果)


那么,如何实现自定义安全键盘呢?大致思路如下:
1)首先捕获系统键盘的弹出、收回通知
2)创建一个更高级别的window挡住系统键盘
3)需要抛出一个 id<UITextInput>textInput 的弱引用切换焦点




下面给出一个简单的安全键盘模型:

  1. @interface WQSafeKeyboard : UIWindow  
  2.   
  3. @property (nonatomic, weak, setter = focusOnTextFiled:) UITextField *textFiled;  
  4. + (WQSafeKeyboard *)deploySafeKeyboard;  
  5. @end  
  6.   
  7.   
  8. @interface WQSafeKeyboard()  
  9.   
  10. @property (nonatomicstrong)WQInterKeyboard *keyboard;  
  11. @end  
  12.   
  13. @implementation WQSafeKeyboard  
  14.   
  15. + (WQSafeKeyboard *)deploySafeKeyboard  
  16. {  
  17.     WQSafeKeyboard *kb = [[WQSafeKeyboard alloc]init];  
  18.     [kb addObserver];  
  19.     return kb;  
  20. }  
  21.   
  22. - (instancetype)init  
  23. {  
  24.     if (self = [super init]) {  
  25.         self.windowLevel = UIWindowLevelAlert;  
  26.         self.frame = CGRectZero;  
  27.         self.rootViewController = self.keyboard;  
  28.     }  
  29.     return self;  
  30. }  
  31.   
  32. - (void)dealloc  
  33. {  
  34.     [[NSNotificationCenter defaultCenter] removeObserver:self];  
  35. }  
  36.   
  37. - (WQInterKeyboard *)keyboard  
  38. {  
  39.     if (!_keyboard) {  
  40.         _keyboard = [[WQInterKeyboard alloc]init];  
  41.     }  
  42.     return _keyboard;  
  43. }  
  44.   
  45. - (void)focusOnTextFiled:(UITextField *)textFiled  
  46. {  
  47.     _textFiled = textFiled;  
  48.     self.keyboard.textField = _textFiled;  
  49. }  
  50.   
  51. - (void)addObserver  
  52. {  
  53.     [[NSNotificationCenter defaultCenter]addObserver:self  
  54.                                             selector:@selector(keyboardWillShow:)  
  55.                                                 name:UIKeyboardWillShowNotification  
  56.                                               object:nil];  
  57.     [[NSNotificationCenter defaultCenter]addObserver:self  
  58.                                             selector:@selector(keyboardWillHide:)  
  59.                                                 name:UIKeyboardWillHideNotification  
  60.                                               object:nil];  
  61. }  
  62.   
  63. - (void)keyboardWillShow:(NSNotification *)notification  
  64. {  
  65.     if (![self.textFiled isFirstResponder]) {  
  66.         return;  
  67.     }  
  68.     [self keyboardAnimationWithNotification:notification];  
  69. }  
  70.   
  71. - (void)keyboardWillHide:(NSNotification *)notification  
  72. {  
  73.     if (![self.textFiled isFirstResponder]) {  
  74.         return;  
  75.     }  
  76.     [self keyboardAnimationWithNotification:notification];  
  77. }  
  78.   
  79. - (void)keyboardAnimationWithNotification:(NSNotification *)notification  
  80. {  
  81.     [self makeKeyAndVisible];  
  82.     NSDictionary *userInfo = [notification userInfo];  
  83.     CGRect kbFrame_end,kbFrame_begin;  
  84.     NSTimeInterval animationDuration;  
  85.     UIViewAnimationCurve animationCurve;  
  86.     [userInfo[UIKeyboardFrameEndUserInfoKey] getValue:&kbFrame_end];  
  87.     [userInfo[UIKeyboardFrameBeginUserInfoKey] getValue:&kbFrame_begin];  
  88.     [userInfo[UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];  
  89.     [userInfo[UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];  
  90.       
  91.     self.frame = [self resizeFrameToAdjust:kbFrame_begin];  
  92.     [UIView animateWithDuration:animationDuration  
  93.                           delay:0  
  94.                         options:(animationCurve<<16)  
  95.                      animations:^{  
  96.                          self.frame = [self resizeFrameToAdjust:kbFrame_end];  
  97.                      }completion:^(BOOL finished) {  
  98.                           
  99.                      }];  
  100.     if ([notification.name isEqualToString:UIKeyboardWillHideNotification]) {  
  101.         [self resignKeyWindow];  
  102.     }  
  103. }  
  104.   
  105. - (CGRect)resizeFrameToAdjust:(CGRect)frame  
  106. {  
  107.     if ([[UIApplication sharedApplication] isStatusBarHidden] )  
  108.         return frame;  
  109.       
  110.     if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {  
  111.         frame = CGRectMake(frame.origin.x,  
  112.                            frame.origin.y - STATUSBAR_HEIGHT,  
  113.                            frame.size.width,  
  114.                            frame.size.height);  
  115.     }  
  116.     return frame;  
  117. }  
  118.   
  119. @end  
0 0
原创粉丝点击