为了让我们的app更加安全,需要数字随机的密码键盘,这就需要自定义键盘了.
大体的思路
自定义键盘思路.png
打乱数组
- (NSMutableArray *)upsetOrderMutableArrary{
NSMutableArray *arrary = [[NSMutableArray alloc] initWithArray:self];
[arrary sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return (NSInteger)arc4random() % 3 - 1;
}];
return arrary;
}
如何获取第一响应者
static __weak id safe_currentFirstResponder;
@implementation UIResponder (ISTSafeKeyboardFirstResponder)
+ (id)safe_currentFirstResponder{
safe_currentFirstResponder = nil;
/*
UIWindow keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView firstResponder = [keyWindow performSelector:@selector(firstResponder)];
*/
//以上方法调用了私有api,审核会被拒绝, 用递归也可以找到第一响应者,但是有点呵呵
// 通过将target设置为nil,让系统自动遍历响应链
// 从而响应链当前第一响应者响应我们自定义的方法
[[UIApplication sharedApplication] sendAction:@selector(safe_findFirstResponder:)
to:nil
from:nil
forEvent:nil];
return safe_currentFirstResponder;
}
- (void)safe_findFirstResponder:(id)sender {
// 第一响应者会响应这个方法,并且将静态变量wty_currentFirstResponder设置为自己
safe_currentFirstResponder = self;
}
@end
最后demo附上
最后的效果图:
屏幕快照 2017-09-06 下午5.24.42.png