登入进行面部识别扫码跳转

1.导入一下数据库在这里插入图片描述

2.创建新文件COM+N
记得勾选Also create XIB file
在这里插入图片描述
3.创建新文件 选择macOS中的Objective-c File文件
注意:⚠️File Type:(为Category)文件 class:为NSString类型
在这里插入图片描述
4.在NSString+regular.h中

//声明
+(BOOL)judgePassWordLegal:(NSString *)_textfile;

5.在NSString+regular.m中

+(BOOL)judgePassWordLegal:(NSString *)_textfile{
    BOOL result = false;
    if ([_textfile length] >= 4){
        // 判断长度大于8位后再接着判断是否同时包含数字和字符
        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:_textfile];
        
    }
    return result;
}

6.在vc.m中

#import "ViewController.h"
#import "oneViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
#import "NSString+regular.h"
@interface ViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField *text1;
@property (nonatomic,strong)UITextField *text2;
@property (nonatomic,strong)LAContext *laCtx;//TouchID管理

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化
    self.laCtx=[[LAContext alloc]init];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(95,90,185,46);
    imageView.image=[UIImage imageNamed:@"1"];
    [self.view addSubview:imageView];
    //手机号
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(15.5,202,46.5,14);
    label.text = @"手机号";
    label.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:15];
    label.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
    [self.view addSubview:label];
    _text1 = [[UITextField alloc] init];
    _text1.frame = CGRectMake(103,203.5,160,11.5);
    
    _text1.placeholder=@"请输入手机号";
    _text1.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
    
    [self.view addSubview:_text1];
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(15,231,345,1);
    view.backgroundColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1];
    [self.view addSubview:view];
    //验证码
    UILabel *label1 = [[UILabel alloc] init];
    label1.frame = CGRectMake(15.5,248,46,14);
    label1.text = @"验证码";
    label1.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:15];
    label1.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
    [self.view addSubview:label1];
    _text2 = [[UITextField alloc] init];
    _text2.frame = CGRectMake(102.5,249.5,160,11.5);
    _text2.placeholder=@"请填写验证码";
    _text2.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
    [self.view addSubview:_text2];
    UIView *view2 = [[UIView alloc] init];
    view2.frame = CGRectMake(15,277,345,1);
    view2.backgroundColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1];
    [self.view addSubview:view2];
    //获取二维码
    UILabel *labell = [[UILabel alloc] init];
    labell.frame = CGRectMake(289.5,249,63,11.5);
    labell.text = @"获取验证码";
    labell.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
    labell.textColor = [UIColor colorWithRed:147/255.0 green:47/255.0 blue:233/255.0 alpha:1];
    [self.view addSubview:labell];
    //点击登入
    UILabel *labell1 = [[UILabel alloc] init];
    labell1.frame = CGRectMake(71,380.5,226,11.5);
    labell1.text = @"点击登录代表您同意《布洛克用户协议》";
    labell1.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:12];
    labell1.textColor = [UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1];
    [self.view addSubview:labell1];
    //按钮
    UIButton *button = [[UIButton alloc] init];
    button.frame = CGRectMake(47.5,319.5,279.5,46);
    [button setTitle:@"立即登录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorNamed:@"#FFFFFF"] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor colorWithRed:147/255.0 green:47/255.0 blue:233/255.0 alpha:1];
    button.layer.cornerRadius = 23;
    //触发
    [button addTarget:self action:@selector(dj) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    
}

-(void)dj{
    //判断
    if (self.text1.text.length==0||self.text2.text.length==0||self.text2.text.length<4||self.text2.text.length>16) {
        //设置提示文字
        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"密码或账号为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil] show];
    }else if (![NSString judgePassWordLegal:self.text2.text]) {
        [[[UIAlertView alloc] initWithTitle:@"提示" message:@"密码错误" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil] show];
    }else{
    //判断是否可以使用TouchID
    if([self.laCtx canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics  error:nil]){
        //申请授权
        [self.laCtx evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics  localizedReason:@"使用您的FaveID/TouchID便如进入下一个视图" reply:^(BOOL success, NSError * _Nullable error) {
            if(success){
                dispatch_async(dispatch_get_main_queue(), ^{
                    oneViewController *one=[oneViewController new];
                   [self presentViewController:one animated:YES completion:nil];
                });
                
            }
        }];
    }else{
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"不支持TouchID/FaceID" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
         [self presentViewController:alert animated:YES completion:nil];
    }
    }
    
    
    
    

}




@end

7.在oneView.h中


#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>


@interface oneViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>
@property (weak, nonatomic) IBOutlet UIView *viewPreview;
@property (weak, nonatomic) IBOutlet UILabel *lblStatus;
@property (weak, nonatomic) IBOutlet UIButton *startBtn;
- (IBAction)startStopReading:(id)sender;

@property (strong, nonatomic) UIView *boxView;
@property (nonatomic) BOOL isReading;
@property (strong, nonatomic) CALayer *scanLayer;

-(BOOL)startReading;
-(void)stopReading;

//捕捉会话
@property (nonatomic, strong) AVCaptureSession *captureSession;
//展示layer
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;


@end

8.在onevie.m中

#import "oneViewController.h"

@interface oneViewController ()

@end

@implementation oneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _captureSession = nil;
    _isReading = NO;
}
- (BOOL)startReading {
    NSError *error;
    
    //1.初始化捕捉设备(AVCaptureDevice),类型为AVMediaTypeVideo
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    //2.用captureDevice创建输入流
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
    if (!input) {
        NSLog(@"%@", [error localizedDescription]);
        return NO;
    }
    
    //3.创建媒体数据输出流
    AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
    
    //4.实例化捕捉会话
    _captureSession = [[AVCaptureSession alloc] init];
    
    //4.1.将输入流添加到会话
    [_captureSession addInput:input];
    
    //4.2.将媒体输出流添加到会话中
    [_captureSession addOutput:captureMetadataOutput];
    
    //5.创建串行队列,并加媒体输出流添加到队列当中
    dispatch_queue_t dispatchQueue;
    dispatchQueue = dispatch_queue_create("myQueue", NULL);
    //5.1.设置代理
    [captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
    
    //5.2.设置输出媒体数据类型为QRCode
    [captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
    
    //6.实例化预览图层
    _videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
    
    //7.设置预览图层填充方式
    [_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    
    //8.设置图层的frame
    [_videoPreviewLayer setFrame:_viewPreview.layer.bounds];
    
    //9.将图层添加到预览view的图层上
    [_viewPreview.layer addSublayer:_videoPreviewLayer];
    
    //10.设置扫描范围
    captureMetadataOutput.rectOfInterest = CGRectMake(0.2f, 0.2f, 0.8f, 0.8f);
    
    //10.1.扫描框
    _boxView = [[UIView alloc] initWithFrame:CGRectMake(_viewPreview.bounds.size.width * 0.2f, _viewPreview.bounds.size.height * 0.2f, _viewPreview.bounds.size.width - _viewPreview.bounds.size.width * 0.4f, _viewPreview.bounds.size.height - _viewPreview.bounds.size.height * 0.4f)];
    _boxView.layer.borderColor = [UIColor greenColor].CGColor;
    _boxView.layer.borderWidth = 1.0f;
    
    [_viewPreview addSubview:_boxView];
    
    //10.2.扫描线
    _scanLayer = [[CALayer alloc] init];
    _scanLayer.frame = CGRectMake(0, 0, _boxView.bounds.size.width, 1);
    _scanLayer.backgroundColor = [UIColor brownColor].CGColor;
    
    [_boxView.layer addSublayer:_scanLayer];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(moveScanLayer:) userInfo:nil repeats:YES];
    
    [timer fire];
    
    //10.开始扫描
    [_captureSession startRunning];
    
    
    return YES;
}

- (IBAction)startStopReading:(id)sender {
    if (!_isReading) {
        if ([self startReading]) {
            [_startBtn setTitle:@"Stop" forState:UIControlStateNormal];
            [_lblStatus setText:@"Scanning for QR Code"];
        }
    }
    else{
        [self stopReading];
        [_startBtn setTitle:@"Start!" forState:UIControlStateNormal];
    }
    
    _isReading = !_isReading;
}
-(void)stopReading{
    
    [_captureSession stopRunning];
    _captureSession = nil;
    [_scanLayer removeFromSuperlayer];
    [_videoPreviewLayer removeFromSuperlayer];
    //进行跳转
    NSString *string=self.lblStatus.text;
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
}

#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    //判断是否有数据
    if (metadataObjects != nil && [metadataObjects count] > 0) {
        AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
        //判断回传的数据类型
        if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
            [_lblStatus performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];
            
            [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
            _isReading = NO;
        }
    }
}

- (void)moveScanLayer:(NSTimer *)timer
{
    CGRect frame = _scanLayer.frame;
    if (_boxView.frame.size.height < _scanLayer.frame.origin.y) {
        frame.origin.y = 0;
        _scanLayer.frame = frame;
    }else{
        
        frame.origin.y += 5;
        
        [UIView animateWithDuration:0.1 animations:^{
            _scanLayer.frame = frame;
        }];
    }
}

- (BOOL)shouldAutorotate
{
    return NO;
}
- (IBAction)button1:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

@end

9.将one.h里面的属性拖进one.xib对应的空键中
在这里插入图片描述
10.info.plist中记得允许面部ID使用和相机权限
在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值