简单的 TouchID demo 的使用 && UIlaterView block的封装 && UIAlertController使用.block的回调

导入  

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 preferredStyleUIAlertControllerStyleAlert];

    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;

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值