给app 添加手势验证

工具类:

.h文件:

#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 delegate:(id<WJTouchIDDelegate>)delegate; 

@end

.m 文件

@implementation WJTouchID 

- (void)startWJTouchIDWithMessage:(NSString *)message  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: {

                        /// TouchID验证失败
                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeFailure)]) {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate WJTouchIDAuthorizeFailure];
                            }];
                        }
                    }
                        break;

                    case LAErrorUserCancel: {
                        ///  取消TouchID验证 (用户点击了取消)
                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorUserCancel)]) {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate WJTouchIDAuthorizeErrorUserCancel];
                            }];
                        }
                    }
                        break;

                    case LAErrorUserFallback: {

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

                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorUserFallback)]) {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate WJTouchIDAuthorizeErrorUserFallback];
                            }];
                        }
                    }
                        break;

                    case LAErrorSystemCancel:{

                        /// 在验证的TouchID的过程中被系统取消 例如突然来电话、按了Home键、锁屏...
                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorSystemCancel)]) {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate WJTouchIDAuthorizeErrorSystemCancel];
                            }];
                        }
                    }
                        break;

                    case LAErrorTouchIDNotEnrolled: {

                        ///  设备没有录入TouchID,无法启用TouchID
                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorTouchIDNotEnrolled)]) {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate WJTouchIDAuthorizeErrorTouchIDNotEnrolled];
                            }];
                        }
                    }
                        break;

                    case LAErrorPasscodeNotSet: {

                        /// 无法启用TouchID,设备没有设置密码
                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorPasscodeNotSet)]) {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate WJTouchIDAuthorizeErrorPasscodeNotSet];
                            }];
                        }
                    }
                        break;

                    case LAErrorTouchIDNotAvailable: {

                        ///  该设备的TouchID无效
                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorTouchIDNotAvailable)]) {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate WJTouchIDAuthorizeErrorTouchIDNotAvailable];
                            }];
                        }
                    }
                        break;

                    case LAErrorTouchIDLockout: {

                        /// 多次连续使用Touch ID失败,Touch ID被锁,需要用户输入密码解锁
                        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: {

                        /// 当前软件被挂起取消了授权 (授权过程中,LAContext对象被释)
                        if ([self.delegate respondsToSelector:@selector(WJTouchIDAuthorizeErrorInvalidContext)]) {
                            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                [self.delegate WJTouchIDAuthorizeErrorInvalidContext];
                            }];
                        }
                    }
                        break;
                }
            }
        }];

    } else {

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

            [self.delegate WJTouchIDIsNotSupport];
        }
    }
}
 

调用对应的代理方法

时间: 2024-11-06 07:13:35

给app 添加手势验证的相关文章

为Azure Web Site 添加ADFS验证支持之二 在代码里使用ADFS

下面我们来创建一个MVC 5.0的ASP.Net程序,并且将它部署到Azure Web Site上 通过Visual Studio 2015创建Web Project 在选择ASP.net模板的地方,更改验证方式   在选择验证方式时选择"Work And School Accounts",在文本框中填入 1.你公司的ADFS的Metadata的地址,这个地址可以找ADFS的管理员要到,通常如以下形式: https://{youradfs.yourcompany.com}/federa

微信小程序 使用HMACSHA1和md5为登陆注册报文添加指纹验证签名

对接口请求报文作指纹验证签名相信在开发中经常碰到, 这次在与java后端一起开发小程序时,就碰到需求对登陆注册请求报文添加指纹验证签名来防止信息被修改 先来看下我们与后端定制签名规则 2.4. 签名规则 原文规则:采用标准的JSON格式,null值字段舍去,按照key值字符串升序排列 例如:{"appId":"1100310061380986","outTradeNo":"1515120685073","timest

移动端测试——APP元素信息[事件]操作API和APP模拟手势高级操作(4)

appium基础API 1.1 APP元素信息操作API 介绍手机端元素信息的获取以及基本的输入操作 前置代码 # 导入driver对象 from appium import webdriver import time # server 启动参数 desired_caps = {} # 设备信息(系统.版本.设备号) desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '9' desired_cap

为MongoDB添加身份验证

MongoDB 默认没有开户身份验证,除非不开放外网访问,否则这种模式极不安全,现纪录添加身份验证步骤如下: 配置创建用户的时候,需要关闭: #security:##副本集之间通信用到的认证文件# keyFile: /home/soft/mongodb-linux-x86_64-rhel62-3.4.2-shard2/mongo-key# clusterAuthMode: "keyFile"##开启身份验证# authorization: "enabled" 因为你

UITableviewCell 点击事件与添加手势的冲突

#pragma mark - 添加手势隐藏键盘 - (void)tabBackground { UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce)]; tap.delegate = self; [tap setNumberOfTouchesRequired:1]; [self.view addGestureRecognizer:tap

UIView添加手势 然后UITableView 添加进这个View 导致UITableView 的单元格点击事件无效

#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIView * v = [[UIView alloc] init

XIB添加手势

首先添加手势控件 其次添加使用对象(不是Outlets是Outlet Collections) 其次便是关联M文件,建立条件出发时执行的方法 OK

李洪强iOS开发之添加手势

李洪强iOS开发之添加手势 02 - 添加手势

上传APP添加视频预览--精简点名

上传APP添加视频预览--精简点名 在为精简点名APP制作视频预览时的坑: 1.视频预览不能太长,也不能太短15-30s就好:我录制的是18s 2.视频的帧数不能太大,也就是说你在录制视频的时候,要慢点录制: 3.上传时可能会说文件的无法载入,请再次上传,这个多数是你网络不好造成的,找个网络好的时候,重新上传: 4.视频的尺寸是有限制的,需要不同设备的尺寸:这里有个技巧,想要不同的尺寸,那么使用不同的设备进行录制 5.采用什么样的软件,其实使用QuickTime+iphone就可以搞定,也不需要