指紋解鎖功能LAContext的使用

通過LAContext的使用席舍,調(diào)用系統(tǒng)指紋解鎖功能來實現(xiàn)APP的解鎖。

代碼封裝
接口文件.h

#import <Foundation/Foundation.h>

@interface AuthenManager : NSObject

- (void)authen:(void (^)(void))start complete:(void (^)(BOOL success, NSInteger errorCode))complete;

@end

實現(xiàn)文件.m

#import "AuthenManager.h"

// 指紋解鎖必須的頭文件
#import <LocalAuthentication/LocalAuthentication.h>

@interface AuthenManager ()

@property (nonatomic, strong) LAContext *context;

@end

@implementation AuthenManager

- (void)authen:(void (^)(void))start complete:(void (^)(BOOL success, NSInteger errorCode))complete
{
    // 開始回調(diào)
    if (start) {
        start();
    }
    
    // 判斷設(shè)備是否支持指紋識別
    NSError *error = nil;
    BOOL isValidTouch = [self.context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error];
    if (isValidTouch)
    {
        NSLog(@"支持指紋識別");
        [self.context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"請按home鍵指紋解鎖" reply:^(BOOL success, NSError * _Nullable error) {
            if (success)
            {
                NSLog(@"驗證成功 刷新主界面");                
            }
            else
            {
                NSLog(@"%@", error.localizedDescription);
                switch (error.code)
                {
                    case LAErrorSystemCancel:
                    {
                        NSLog(@"系統(tǒng)取消授權(quán)哮笆,如其他APP切入");
                        break;
                    }
                    case LAErrorUserCancel:
                    {
                        NSLog(@"用戶取消驗證Touch ID");
                        break;
                    }
                    case LAErrorAuthenticationFailed:
                    {
                        NSLog(@"授權(quán)失敗");
                        break;
                    }
                    case LAErrorPasscodeNotSet:
                    {
                        NSLog(@"系統(tǒng)未設(shè)置密碼");
                        break;
                    }
                    case LAErrorTouchIDNotAvailable:
                    {
                        NSLog(@"設(shè)備Touch ID不可用来颤,例如未打開");
                        break;
                    }
                    case LAErrorTouchIDNotEnrolled:
                    {
                        NSLog(@"設(shè)備Touch ID不可用汰扭,用戶未錄入");
                        break;
                    }
                    case LAErrorUserFallback:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"用戶選擇輸入密碼,切換主線程處理");
                        }];
                        break;
                    }
                    default:
                    {
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"其他情況脚曾,切換主線程處理");
                        }];
                        break;
                    }
                }
            }
            
            // 成功回調(diào)
            if (complete) {
                complete(success, error.code);
            }
        }];
    }
    else
    {
        NSLog(@"不支持指紋識別");
        switch (error.code)
        {
            case LAErrorTouchIDNotEnrolled:
            {
                NSLog(@"TouchID is not enrolled");
                break;
            }
            case LAErrorPasscodeNotSet:
            {
                NSLog(@"A passcode has not been set");
                break;
            }
            default:
            {
                NSLog(@"TouchID not available");
                break;
            }
        }
        NSLog(@"%@", error.localizedDescription);
        
        // 失敗回調(diào)
        if (complete) {
            complete(NO, error.code);
        }
    }
}

- (LAContext *)context
{
    if (_context == nil) {
        // 創(chuàng)建LAContext
        _context = [LAContext new];
        // 這個設(shè)置的使用密碼的字體,當text=@""時启具,按鈕將被隱藏本讥,也設(shè)置指紋輸入失敗之后的彈出框的選項
        _context.localizedFallbackTitle = @"密碼解鎖";
        // 這個設(shè)置的取消按鈕的字體
        _context.localizedCancelTitle = @"取消";
    }
    return _context;
}

@end

封裝類使用

#import "ViewController.h"
// 導(dǎo)入封裝類頭文件
#import "AuthenManager.h"

@interface ViewController ()

@property (nonatomic, strong) AuthenManager *authenManager;
@property (nonatomic, strong) UILabel *authenLabel;
@property (nonatomic, strong) NSString *message;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.navigationItem.title = @"指紋解鎖";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"authen" style:UIBarButtonItemStyleDone target:self action:@selector(authenClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"alert" style:UIBarButtonItemStyleDone target:self action:@selector(alertClick)];
    
    self.authenLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 40.0)];
    [self.view addSubview:self.authenLabel];
    self.authenLabel.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.2];
    self.authenLabel.textAlignment = NSTextAlignmentCenter;
    self.authenLabel.text = @"等待解鎖~";
    //
    self.message = @"請先進行指紋解鎖!";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)authenClick
{
    if (self.authenManager == nil) {
        self.authenManager = [[AuthenManager alloc] init];
    }
    
    ViewController __weak *weakSelf = self;
    [self.authenManager authen:^{
        weakSelf.navigationItem.leftBarButtonItem.enabled = NO;
        weakSelf.navigationItem.rightBarButtonItem.enabled = NO;
        
        weakSelf.authenLabel.text = @"正在進行【指紋】解鎖……";
    } complete:^(BOOL success, NSInteger errorCode) {
        weakSelf.navigationItem.leftBarButtonItem.enabled = YES;
        weakSelf.navigationItem.rightBarButtonItem.enabled = YES;
        
        if (success) {
            weakSelf.authenLabel.text = @"【指紋】解鎖成功鲁冯!";
            weakSelf.message = @"已經(jīng)進行指紋解鎖拷沸!可以進行一下步操作了~";
        } else {
            weakSelf.authenLabel.text = @"【指紋】解鎖失敗薯演!";
            weakSelf.message = @"【指紋】解鎖失斪采帧!";
        }
    }];
}

- (void)alertClick
{
    [[[UIAlertView alloc] initWithTitle:nil message:self.message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"知道了", nil] show];
}

@end
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末跨扮,一起剝皮案震驚了整個濱河市序无,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌衡创,老刑警劉巖帝嗡,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異璃氢,居然都是意外死亡哟玷,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進店門一也,熙熙樓的掌柜王于貴愁眉苦臉地迎上來巢寡,“玉大人,你說我怎么就攤上這事椰苟∫衷拢” “怎么了?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵舆蝴,是天一觀的道長爪幻。 經(jīng)常有香客問我,道長须误,這世上最難降的妖魔是什么挨稿? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮京痢,結(jié)果婚禮上奶甘,老公的妹妹穿的比我還像新娘。我一直安慰自己祭椰,他們只是感情好臭家,可當我...
    茶點故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布疲陕。 她就那樣靜靜地躺著,像睡著了一般钉赁。 火紅的嫁衣襯著肌膚如雪蹄殃。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天你踩,我揣著相機與錄音诅岩,去河邊找鬼。 笑死带膜,一個胖子當著我的面吹牛吩谦,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播膝藕,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼式廷,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了芭挽?” 一聲冷哼從身側(cè)響起滑废,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎袜爪,沒想到半個月后策严,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡饿敲,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年妻导,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片怀各。...
    茶點故事閱讀 40,001評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡倔韭,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出瓢对,到底是詐尸還是另有隱情寿酌,我是刑警寧澤,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布硕蛹,位于F島的核電站醇疼,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏法焰。R本人自食惡果不足惜秧荆,卻給世界環(huán)境...
    茶點故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望埃仪。 院中可真熱鬧乙濒,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至甘有,卻和暖如春诉儒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背亏掀。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工忱反, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人幌氮。 一個月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓缭受,卻偏偏與公主長得像胁澳,于是被迫代替她去往敵國和親该互。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,955評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 1韭畸、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明先生_X自主閱讀 15,981評論 3 119
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,144評論 25 707
  • 男人宇智,已婚或者未婚,應(yīng)該都不滿足于婚姻感情的需求胰丁,大多還需要其他外在給予的肯定或者崇拜随橘。浙中情感訴求,往往自私而自...
    S的神秘紀念閱讀 885評論 1 1
  • 讀詩觀荷戲題 名家珠玉匯長河锦庸,萬頃風荷傳老婆机蔗。 并蒂花前枝連理,沉香羞蕊愧不如甘萧。 浮生半日許煙霞萝嘁,借得風荷意甚嘉。...
    簡語星空閱讀 485評論 5 3
  • 珊珊的小家
    珊珊921閱讀 235評論 0 0