ios TouchID验证

//------------------------------使用----------------------------------

  WJTouchID *touchid = [[WJTouchID alloc]init];

    

    [touchid startWJTouchIDWithMessage:WJNotice(@"自定义信息", @"The Custom Message") fallbackTitle:WJNotice(@"按钮标题", @"Fallback Title") delegate:self];

    

    self.touchID = touchid;

//------------------------------声明----------------------------------

#import <LocalAuthentication/LocalAuthentication.h>

#if TARGET_IPHONE_SIMULATOR

#define WJNotice(Chinese,English) [[[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0] isEqualToString:@"zh-Hans-US"] ? Chinese : English

#elif TARGET_OS_IPHONE

#define WJNotice(Chinese,English) [[[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0] isEqualToString:@"zh-Hans-CN"] ? Chinese : English

#endif

 

@class WJTouchID;

 

@protocol WJTouchIDDelegate <NSObject>

 

@required

 

/**

 *  TouchID验证成功

 *

 *  (English Comments) Authentication Successul

 */

- (void)WJTouchIDAuthorizeSuccess;

 

/** 

 *  TouchID验证失败

 *

 *  (English Comments) Authentication Failure

 */

- (void)WJTouchIDAuthorizeFailure;

 

@optional

/**

 *  取消TouchID验证 (用户点击了取消)

 *

 *  (English Comments) Authentication was canceled by user (e.g. tapped Cancel button).

 */

- (void)WJTouchIDAuthorizeErrorUserCancel;

 

/**

 *  TouchID对话框中点击输入密码按钮

 *

 *  (English Comments) User tapped the fallback button

 */

- (void)WJTouchIDAuthorizeErrorUserFallback;

 

/** 

 *  在验证的TouchID的过程中被系统取消 例如突然来电话、按了Home键、锁屏...

 *

 *  (English Comments) Authentication was canceled by system (e.g. another application went to foreground).

 */

- (void)WJTouchIDAuthorizeErrorSystemCancel;

 

/** 

 *  无法启用TouchID,设备没有设置密码

 *

 *  (English Comments) Authentication could not start, because passcode is not set on the device.

 */

- (void)WJTouchIDAuthorizeErrorPasscodeNotSet;

 

/**

 *  设备没有录入TouchID,无法启用TouchID

 *

 *  (English Comments) Authentication could not start, because Touch ID has no enrolled fingers

 */

- (void)WJTouchIDAuthorizeErrorTouchIDNotEnrolled;

 

/**

 *  该设备的TouchID无效

 *

 *  (English Comments) Authentication could not start, because Touch ID is not available on the device.

 */

- (void)WJTouchIDAuthorizeErrorTouchIDNotAvailable;

 

/** 

 *  多次连续使用Touch ID失败,Touch ID被锁,需要用户输入密码解锁

 *

 *  (English Comments) Authentication was not successful, because there were too many failed Touch ID attempts and Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite.

 *

 */

- (void)WJTouchIDAuthorizeErrorTouchIDLockout;

 

/**

 *  当前软件被挂起取消了授权(如突然来了电话,应用进入前台)

 *

 *  (English Comments) Authentication was canceled by application (e.g. invalidate was called while authentication was inprogress).

 *

 */

- (void)WJTouchIDAuthorizeErrorAppCancel;

 

/** 

 *  当前软件被挂起取消了授权 (授权过程中,LAContext对象被释)

 *

 *  (English Comments) LAContext passed to this call has been previously invalidated.

 */

- (void)WJTouchIDAuthorizeErrorInvalidContext;

 

/**

 *  当前设备不支持指纹识别

 *

 *  (English Comments) The current device does not support fingerprint identification

 */

-(void)WJTouchIDIsNotSupport;

 

@end

 

@interface WJTouchID : LAContext

 

@property (nonatomic, weak) id<WJTouchIDDelegate> delegate;

 

/**

 *  发起TouchID验证 (Initiate TouchID validation)

 *

 *  @param message 提示框需要显示的信息 默认为:输入密码 (Fallback button title. Default is "Enter Password")

 */

- (void)startWJTouchIDWithMessage:(NSString *)message fallbackTitle:(NSString *)fallbackTitle delegate:(id<WJTouchIDDelegate>)delegate; 

 

@end

//------------------------------------------实现-------------------------------------------

#import "WJTouchID.h"

 

@interface WJTouchID ()

 

@end

 

@implementation WJTouchID 

 

- (void)startWJTouchIDWithMessage:(NSString *)message fallbackTitle:(NSString *)fallbackTitle delegate:(id<WJTouchIDDelegate>)delegate {

    

    LAContext *context = [[LAContext alloc]init];

    

    context.localizedFallbackTitle = fallbackTitle;

    

    NSError *error = nil;

    

    self.delegate = delegate;

    

    NSAssert(self.delegate != nil, WJNotice(@"WJTouchIDDelegate 不能为空", @"WJTouchIDDelegate must be non-nil"));

    

    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {

        

        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:message == nil ? WJNotice(@"默认提示信息", @"The Default Message") : message reply:^(BOOL success, NSError * _Nullable error) { 

            

            if (success) {

                

                if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeSuccess)]) {

                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                        [self.delegate WJTouchIDAuthorizeSuccess];

                    }];

                    

                }

            } else if (error) {

                

                switch (error.code) {

                        

                    case LAErrorAuthenticationFailed: {

                        

                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeFailure)]) {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                [self.delegate WJTouchIDAuthorizeFailure];

                            }];

                        }

                    }

                        break;

                        

                    case LAErrorUserCancel: {

                        

                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorUserCancel)]) {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                [self.delegate WJTouchIDAuthorizeErrorUserCancel];

                            }];

                        }

                    }

                        break;

                        

                    case LAErrorUserFallback: {

                        

                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorUserFallback)]) {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                [self.delegate WJTouchIDAuthorizeErrorUserFallback];

                            }];

                        }

                    }

                        break;

                        

                    case LAErrorSystemCancel:{

                        

                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorSystemCancel)]) {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                [self.delegate WJTouchIDAuthorizeErrorSystemCancel];

                            }];

                        }

                    }

                        break;

                        

                    case LAErrorTouchIDNotEnrolled: {

                        

                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorTouchIDNotEnrolled)]) {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                [self.delegate WJTouchIDAuthorizeErrorTouchIDNotEnrolled];

                            }];

                        }

                    }

                        break;

                        

                    case LAErrorPasscodeNotSet: {

                        

                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorPasscodeNotSet)]) {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                [self.delegate WJTouchIDAuthorizeErrorPasscodeNotSet];

                            }];

                        }

                    }

                        break;

                        

                    case LAErrorTouchIDNotAvailable: {

                        

                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorTouchIDNotAvailable)]) {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                [self.delegate WJTouchIDAuthorizeErrorTouchIDNotAvailable];

                            }];

                        }

                    }

                        break;

                        

                    case LAErrorTouchIDLockout: {

                        

                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorTouchIDLockout)]) {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                [self.delegate WJTouchIDAuthorizeErrorTouchIDLockout];

                            }];

                        }

                    }

                        break;

                        

                    case LAErrorAppCancel:  {

                        

                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorAppCancel)]) {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                [self.delegate WJTouchIDAuthorizeErrorAppCancel];

                            }];

                        }

                    }

                        break;

                        

                    case LAErrorInvalidContext: {

                        

                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorInvalidContext)]) {

                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                                [self.delegate WJTouchIDAuthorizeErrorInvalidContext];

                            }];

                        }

                    }

                        break;

                }

            }

        }];

        

    } else {

        

        if ([self.delegate respondsToSelector:@selector(WJTouchIDIsNotSupport)]) {

            

            [self.delegate WJTouchIDIsNotSupport];

        }

    }

}

 

@end

// 来着网络项目代码

原文地址:https://www.cnblogs.com/supersr/p/6899461.html