LocalAuthentication
LAContext * context = [[LAContextalloc]init];
BOOL success;
NSError *error;
success = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometricserror:&error];
if (!success) {
//不支持touch ID跳到设置页面进行设置
LZAlterView * alterView = [[LZAlterViewalloc]initWithTitle:@"不支持touch ID 跳到设置页面进行设置"message:nilcancelButtonTitle:@"Cancel"otherButtonTitles:@"OK"block:^(NSInteger buttonIndex) {
if (buttonIndex ==1) {
NSURL *url = [NSURLURLWithString:@"prefs:root"];
if ([[UIApplicationsharedApplication]canOpenURL:url]) {
[[UIApplicationsharedApplication]openURL:url];
} else{
NSLog(@"你跳不过去的");
}
}
}];
[alterView show];
} else {
//支持touch ID
UISwitch * sw = [[UISwitchalloc]initWithFrame:CGRectMake(100,100,50,30)];
BOOL on = [[NSUserDefaultsstandardUserDefaults]objectForKey:@"Touch"];
sw.on = on;
[sw addTarget:selfaction:@selector(touchAction:)forControlEvents:UIControlEventValueChanged];
[self.viewaddSubview:sw];
}
- (void)touchAction:(UISwitch *)sw{
LAContext *context = [[LAContextalloc]init];
NSString *remindString = sw.on ?@"是否开启指纹登录":@"是否取消指纹登录";
context.localizedFallbackTitle =@"参阅看资料。说有用,但是没有看到效果";
__block NSString *tempString =@"";
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometricslocalizedReason:remindStringreply:^(BOOL success,NSError *_Nullable error) {
if (success) {
if (sw.on) {
[[NSUserDefaultsstandardUserDefaults]setBool:YESforKey:@"Touch"];
[[NSUserDefaultsstandardUserDefaults]synchronize];
NSLog(@"添加指纹解锁");
}
else{
[[NSUserDefaultsstandardUserDefaults]removeObjectForKey:@"Touch"];
[[NSUserDefaultsstandardUserDefaults]synchronize];
NSLog(@"取消指纹解锁");
}
} else {
switch (error.code) {
caseLAErrorAuthenticationFailed:
tempString = @"指纹校验失败";
break;
casekLAErrorUserCancel:
tempString = @"用户点击取消";
break;
casekLAErrorUserFallback:
tempString = @"用户回退";
break;
casekLAErrorSystemCancel:
tempString = @"系统取消";
break;
casekLAErrorPasscodeNotSet:
tempString = @"密码未设置";
break;
casekLAErrorTouchIDNotAvailable:
tempString = @"指纹不正确";
break;
casekLAErrorTouchIDLockout:
tempString = @"Touch ID被锁定";
break;
casekLAErrorAppCancel:
tempString = @"APP取消";
break;
casekLAErrorInvalidContext:
tempString = @"其他错误";
break;
default:
break;
}
[selfexMainQueue:^{
sw.on =!sw.on;
LZAlterView * alterView = [[LZAlterViewalloc]initWithTitle:tempStringmessage:nilcancelButtonTitle:@"Cancel"otherButtonTitles:@"OK"block:^(NSInteger buttonIndex) {
}];
[alterView show];
}];
}
}];
}
- (void)exMainQueue:(void (^)())queue {
dispatch_async(dispatch_get_main_queue(), queue);
}
BOOL isTouch = [[NSUserDefaultsstandardUserDefaults]objectForKey:@"Touch"];
if (isTouch) {
LAContext *context = [[LAContextalloc]init];
context.localizedFallbackTitle =@"参阅看资料。说有用,但是没有看到效果";
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:@"使用touch ID来打开"
reply:^(BOOL success,NSError *authenticationError) {
if (success) {
//push视图
[selfexMainQueue:^{
[selfsetAction:nil];
}];
} else {
}
}];
}
}
- (void)exMainQueue:(void (^)())queue {
dispatch_async(dispatch_get_main_queue(), queue);
}
typedef void (^TouchBlock)(NSInteger);
@interface LZAlterView : UIAlertView
@property(nonatomic,copy)TouchBlock block;
//需要自定义初始化方法,调用Block
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString*)otherButtonTitles
block:(TouchBlock)block;
- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString*)otherButtonTitles block:(TouchBlock)block{
self = [superinitWithTitle:title message:message delegate:selfcancelButtonTitle:cancelButtonTitleotherButtonTitles:otherButtonTitles,nil];//注意这里初始化父类的
if (self) {
self.block = block;
}
returnself;
}
//#pragma mark -AlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//这里调用函数指针_block(要传进来的参数);
_block(buttonIndex);
}
+ (void)showAlertType:(YXAlertViewType )alertType WithTitle:(NSString *)title message:(NSString *)message cancel:(NSString *)cancel sure:(NSString *)sure cancelBlock:(void(^)(id object))cancelBlock sureBlock:(void(^)(id object))sureBlock;
//系统alert
+ (void)showAlertType:(YXAlertViewType )alertType WithTitle:(NSString *)title message:(NSString *)message cancel:(NSString *)cancel sure:(NSString *)sure cancelBlock:(void(^)(id object))cancelBlock sureBlock:(void(^)(id object))sureBlock{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle: UIAlertControllerStyleAlert];
if(cancel){
[alert addAction:[UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
if(cancelBlock){
cancelBlock(nil);
}
}]];
}
if(sure){
[alert addAction:[UIAlertAction actionWithTitle:sure style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
if(sureBlock){
sureBlock(nil);
}
}]];
}
UIWindow * window = [UIApplication sharedApplication].keyWindow;
UIViewController * topVC = [window currentViewController];
[topVC presentViewController:alert animated:YES completion:^{
}];
}
- (UIViewController*) topMostController
{
UIViewController *topController = [self rootViewController];
// Getting topMost ViewController
while ([topController presentedViewController]) topController = [topController presentedViewController];
// Returning topMost ViewController
return topController;
}
- (UIViewController*)currentViewController;
{
UIViewController *currentViewController = [self topMostController];
while ([currentViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)currentViewController topViewController])
currentViewController = [(UINavigationController*)currentViewController topViewController];
return currentViewController;
}