iOS8以后使用系统TouchID功能

2017年02月09日 iOS 12条评论 阅读5485次

iOS8以后,苹果公开了TouchID的验证功能,使得自己开发的APP能够更好更方便地保护用户信息及数据。

下面是使用TouchID验证的全部代码。


#import <LocalAuthentication/LocalAuthentication.h>
- (void)beginTouch {
    LAContext *context = [[LAContext alloc]init];
    NSError *err = nil;
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&err]) {
        if (err) {
            NSLog(@"%@", err);
            return;
        }
        context.localizedFallbackTitle = @"使用密码";
        context.localizedCancelTitle= @"取消";
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"现在按上指纹" reply:^(BOOL success, NSError * _Nullable error) {
            if (error) {
                NSLog(@"%@", error);
                switch (error.code) {
                    case LAErrorAuthenticationFailed:
                        NSLog(@"LAErrorAuthenticationFailed:连续三次指纹认证失败!");
                        break;
                    case LAErrorUserCancel:
                        NSLog(@"LAErrorUserCancel:用户已取消!");
                        break;
                    case LAErrorUserFallback:
                        NSLog(@"LAErrorUserFallback:用户选择使用Fallback的功能!");
                        break;
                    case LAErrorSystemCancel:
                        NSLog(@"LAErrorSystemCancel:TouchID对话框被系统取消,例如按下Home或者电源键进入后台!");
                        break;
                    case LAErrorPasscodeNotSet:
                        NSLog(@"LAErrorPasscodeNotSet:设备系统未设置密码!");
                        break;
                    case LAErrorTouchIDNotAvailable:
                        NSLog(@"LAErrorTouchIDNotAvailable:设备未设置Touch ID!");
                        break;
                    case LAErrorTouchIDNotEnrolled:
                        NSLog(@"LAErrorTouchIDNotEnrolled:用户未录入指纹!");
                        break;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
                    case LAErrorTouchIDLockout:
                        NSLog(@"LAErrorTouchIDLockout:Touch ID被锁,需要用户输入密码解锁!");
                        break;
                    case LAErrorAppCancel:
                        NSLog(@"LAErrorAppCancel:如突然来了电话,电话应用进入前台,APP被挂起啦!");
                        break;
                    case LAErrorInvalidContext:
                        NSLog(@"LAErrorInvalidContext:LAContext传递给这个调用之前已经失效!");
                        break;
#else
#endif
                    default:
                        break;
                }
                return;
            }
            if (success) {
                NSLog(@"指纹认证成功!");
            } else {
                NSLog(@"指纹认证失败!");
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                // do something on the UI thread
            });
        }];
    } else {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert" message:@"设备没有指纹认证功能" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertController addAction:action];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}


值得注意的是当用户按下指纹取得到指纹认证结果时,需要在主线程更新UI。

分享本文至:

WRITTEN BY

avatar
本文标签:iOSTouchID
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

12 条评论

  1. avatar playunreal

    <a href="http://www.ipodammo.com/converse-rosa-and-lilla-obuvi">converse rosa and lilla</a> <a href="http://www.bezpozyczka.com/nike-air-max-1-ultra-essential-red-shoesj">nike air max 1 ultra essential red</a> <a href="http://www.ajhoweaboutpt.com/women-air-jordan-low-future-black-shoesl">women air jordan low future black</a> <a href="http://www.rawrapture.com/mens-purple-long-sleeve-polo-poloh">mens purple long sleeve polo</a> <a href="http://www.staholidays.com/nike-air-max-270-flyknit-m%C3%A6nd-lyser%C3%B8d-hvid-fl%C3%A5den-bl%C3%A5-skok">nike air max 270 flyknit m忙nd lyser酶d hvid fl氓den bl氓</a> <a href="http://www.gunsrusoregon.com/nike-air-jordan-1-retro-wei%C3%9F-schuheh">nike air jordan 1 retro wei脽</a>

  2. avatar cmcheechalk

    <a href="http://www.sguerradesign.com/dam%C3%A4nner-nike-roshe-run-speckle-gold-orangen-schuhej">dam盲nner nike roshe run speckle gold orangen</a> <a href="http://www.zeneventsja.com/oakland-raiders-urban-camo-hat-hath">oakland raiders urban camo hat</a> <a href="http://www.adharaweb.com/adidas-neo-leisure-violet-rouge-chaussuresh">adidas neo leisure violet rouge</a> <a href="http://www.jrelselaw.com/nike-kd-9-rosso-rosa-scarpeq">nike kd 9 rosso rosa</a> <a href="http://www.intellalegal.com/ua-micro-g-torch-red-purple-shoesj">ua micro g torch red purple</a> <a href="http://www.carmelscoffee.com/herre-nike-air-huarache-alle-hvid-skoi">herre nike air huarache alle hvid</a>
    cmcheechalk http://www.cmcheechalk.com/

  3. avatar amarthur

    <a href="http://www.getawayoffer.com/nike-air-max-dam%C3%A4nner-basketballe-schuhe-chaussuresf">nike air max dam盲nner basketballe schuhe</a> <a href="http://www.lbsportsguys.com/nike-vomero-mennns-11-gr%C3%A5-scarpef">nike vomero mennns 11 gr氓</a> <a href="http://www.thesleeperson.com/dodgers-22-clayton-kershaw-blue-flexbase-authentic-collection-stitched-mlb-jersey-nfls">dodgers 22 clayton kershaw blue flexbase authentic collection stitched mlb jersey</a> <a href="http://www.ubcfellowship.com/ralph-lauren-pony-polo-orange-fit-womens-polot">ralph lauren pony polo orange fit womens</a> <a href="http://www.vipartonline.com/anthony-miller-mens-limited-navy-blue-jersey-nike-nfl-chicago-bears-tank-top-suit-17-nflz">anthony miller mens limited navy blue jersey nike nfl chicago bears tank top suit 17</a> <a href="http://www.zumbajill.com/kvinners-asics-gel-quantum-360-s%C3%B8lv-gull-kurpesc">kvinners asics gel quantum 360 s酶lv gull</a>
    [url=http://www.amarthur.com/]amarthur[/url]

  4. avatar ankaralimnet

    <a href="http://www.rcrocs.com/nike-air-max-2017-wei%C3%9F-schuhek">nike air max 2017 wei脽</a> <a href="http://www.moreheaddotts.com/adidas-nba-houston-rockets-34-hakeem-olajuwon-soul-swingman-blue-jersey-nflh">adidas nba houston rockets 34 hakeem olajuwon soul swingman blue jersey</a> <a href="http://www.qgchsmrt.com/adidas-messi-15.3-fg-r%C3%B8d-svart-grass-gr%C3%B8nn-obuvi">adidas messi 15.3 fg r酶d svart grass gr酶nn</a> <a href="http://www.firagandia.com/adidas-porsche-blue-shoesj">adidas porsche blue</a> <a href="http://www.lmladeco.com/adidas-nitrocharge-3.0-fg-orange-black-white-shoesl">adidas nitrocharge 3.0 fg orange black white</a> <a href="http://www.brinkmanmdc.com/ralph-lauren-cotton-long-sleeve-poloh">ralph lauren cotton long sleeve</a>
    ankaralimnet http://www.ankaralimnet.com/

  5. avatar PDM系统

    IOS开发比安卓的还是有不少技术含量的

  6. avatar 宁波PLM

    这文章值得学习一下

  7. avatar PDM系统

    这么好的文章值得学习

  8. avatar 盘古科技

    这真是好东西  认真学习一下

  9. avatar 老董优惠码

    不错 感谢分享

  10. avatar 轿车托运公司

    好东西 点个赞

欢迎留言




用户登录

sitemap