1. 程式人生 > >登入判斷 touchID/faceID 二維碼掃描及跳轉

登入判斷 touchID/faceID 二維碼掃描及跳轉

登入正則判斷: 先新建一個類別 NSString+regular.h .h裡寫入 -(BOOL)judgePassWordLegal; .m裡寫入

-(BOOL)judgePassWordLegal{
    BOOL result = false;
    if ([self length] >= 4){
        // 判斷長度大於8位後再接著判斷是否同時包含數字和字元
        NSString * regex = @"^[0-9A-Za-z]{4,16}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:self];
    }
    return result;
}

判斷提示框 先匯入 MBProgressHUD SDK 匯入MBProgressHUD的標頭檔案


#import "MBProgressHUD/MBProgressHUD.h"

寫一個巨集定義


#define G_Window [UIApplication sharedApplication].delegate.window

如果不符合要求就彈出提示框

if([self.mmTEXT.text judgePassWordLegal]==NO){
            MBProgressHUD *hud = [[MBProgressHUD alloc]initWithWindow:G_Window];
            //新增到視窗上
            [G_Window addSubview:hud];
            //設定隱藏自動移除
            hud.removeFromSuperViewOnHide = YES;
            //設定文字樣式的提示框
            hud.mode = MBProgressHUDModeText ;
            //設定文字
            hud.labelText = @"長度是4-16位";
            //顯示提示框
            [hud show:YES];
            //設定2秒後自動隱藏
            [hud hide:YES afterDelay:2.0];
        }

touchID/faceID:

先匯入標頭檔案 在建立屬性

#import <LocalAuthentication/LocalAuthentication.h>

@property (nonatomic , strong)LAContext *context;

判斷是否使用touchID/faceID 支援後跳轉 不支援彈出提示

if ([self.context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
        
[self.context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"進入" reply:^(BOOL success, NSError * _Nullable error) {
  if (success) {
        dispatch_async(dispatch_get_main_queue(), ^{
                    
                    secondViewController *two = [[secondViewController alloc]init];
                    [self presentViewController:two animated:YES completion:nil];
                    });
              }
        }];
  }
    else{
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"不支援touchID/FaceID" preferredStyle:UIAlertControllerStyleAlert];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:two animated:YES completion:nil];
        
    }

二維碼掃描: 先匯入二維碼掃描QRcode

#import "secondViewController.h"
#import "HMScannerController.h"
@interface secondViewController (){
    
}
@property (weak, nonatomic) IBOutlet UITextField *text;
- (IBAction)click:(id)sender;


@end

@implementation secondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *cardName = @"天涯刀哥 - 傅紅雪";
    UIImage *avatar = [UIImage imageNamed:@"avatar"];
    
    [HMScannerController cardImageWithCardName:cardName avatar:avatar scale:0.2 completion:^(UIImage *image) {
       
    }];
}

- (IBAction)click:(id)sender {
    NSString *cardName = @"天涯刀哥 - 傅紅雪";
    UIImage *avatar = [UIImage imageNamed:@"avatar"];
    
    HMScannerController *scanner = [HMScannerController scannerWithCardName:cardName avatar:avatar completion:^(NSString *stringValue) {
        
        self.text.text = stringValue;
        NSURL *url =[NSURL URLWithString:self.text.text];
        [[UIApplication sharedApplication] openURL:url options:nil completionHandler:nil];
    }];
    
    [scanner setTitleColor:[UIColor whiteColor] tintColor:[UIColor greenColor]];
    
    [self showDetailViewController:scanner sender:nil];
    
}