iOS8以后使用系统TouchID功能

2017年02月09日 iOS 18条评论 阅读3049次

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
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

18 条评论

  1. <a href="http://www.iremaxvn.com/billig-nike-vapormax-dam%C3%A4nner-rosa-skod">billig nike vapormax dam盲nner rosa</a> <a href="http://www.multinutwraps.com/philadelphia-flyers-jersey-nflc">philadelphia flyers jersey</a> <a href="http://www.vipartonline.com/buy-burberry-belt-online-india-beltr">buy burberry belt online india</a> <a href="http://www.wantaoren.com/ray-ban-rb4098-jackie-ohh-ii-sunglasses-shiny-black-egg-sunglassesc">ray ban rb4098 jackie ohh ii sunglasses shiny black egg</a> <a href="http://www.tokodanaya.com/orlando-magic-white-blue-tisa-snapback-cap-style-2-handsome-style-hatg">orlando magic white blue tisa snapback cap style 2 handsome style</a> <a href="http://www.dinerwareoc.com/ray-ban-rb2140-wayfarer-sunglasses-tortoise-frame-brown-polarized-sunglassesd">ray ban rb2140 wayfarer sunglasses tortoise frame brown polarized</a>
    nanainchief http://www.nanainchief.com/

  2. <a href="http://www.freizeittrend.com/nike-zoom-winflo-3-dam%C3%A4nner-grau-rot-schuhea">nike zoom winflo 3 dam盲nner grau rot</a> <a href="http://www.instanalo.com/nike-solarsoft-mule-blanco-azul-scarpeb">nike solarsoft mule blanco azul</a> <a href="http://www.olafhuth.com/womens-nike-huarache-utility-black-denmark-shoeso">womens nike huarache utility black denmark</a> <a href="http://www.pecanfirewood.com/nike-air-force-1-mid-wheat-kvinners-kurpesb">nike air force 1 mid wheat kvinners</a> <a href="http://www.ezzolawaz.com/nike-free-run-2-all-black-for-sale-zapatost">nike free run 2 all black for sale</a> <a href="http://www.suchmeldungen.com/girls-high-converse-shoesz">girls high converse</a>
    bootsoffprice http://www.bootsoffprice.com/

  3. <a href="http://www.swnblog.com/chicago-bulls-michael-jordan-hoodie-new-adult-size-medium-other-nflv">chicago bulls michael jordan hoodie new adult size medium other</a> <a href="http://www.weimpactmds.com/nike-zoom-clear-out-1-red-gold-skof">nike zoom clear out 1 red gold</a> <a href="http://www.relsflorida.com/nike-free-run-3-5.0-white-skob">nike free run 3 5.0 white</a> <a href="http://www.votuongluan.com/cincinnati-reds-hats-for-sale-wholesalers-hata">cincinnati reds hats for sale wholesalers</a> <a href="http://www.hanayaflowers.com/green-womens-nike-cortez-shoes-runningf">green womens nike cortez shoes</a> <a href="http://www.jlchatlaw.com/jordan-11-hvid-light-bl%C3%A5-schuhea">jordan 11 hvid light bl氓</a>
    theclaydoctor http://www.theclaydoctor.com/

  4. <a href="http://www.kainjipark.com/youth-jason-kelce-limited-camo-nike-jersey-nfl-philadelphia-eagles-62-rush-realtree-nflr">youth jason kelce limited camo nike jersey nfl philadelphia eagles 62 rush realtree</a> <a href="http://www.lapthelakes.com/zalando-zapatillas-adidas-zx-flux-skoa">zalando zapatillas adidas zx flux</a> <a href="http://www.delmaroffice.com/jordan-future-sport-blue-reflective-sneakere">jordan future sport blue reflective</a> <a href="http://www.medieneinsatz.com/adidas-pr%C3%B8dator-instinct-ag-svart-kurpesa">adidas pr酶dator instinct ag svart</a> <a href="http://www.linexcanton.com/air-jordan-6-varsity-red-2017-events-sneakerb">air jordan 6 varsity red 2017 events</a> <a href="http://www.saskostomy.com/nike-air-max-tailwind-6-green-purple-shoesg">nike air max tailwind 6 green purple</a>
    cloudsmartin http://www.cloudsmartin.com/

  5. <a href="http://www.agichipay.com/florida-panthers-snapback-black-hatb">florida panthers snapback black</a> <a href="http://www.millsbulb.com/adidas-porsche-design-iii-coral-runninge">adidas porsche design iii coral</a> <a href="http://www.hamshrd.com/maschio-air-yeezy-ii-2-sp-max-90-verde-rosso-schuhea">maschio air yeezy ii 2 sp max 90 verde rosso</a> <a href="http://www.lroyroofing.com/hommes-nike-flynit-air-max-vert-bleu-scarpeb">hommes nike flynit air max vert bleu</a> <a href="http://www.golfbodyshop.com/nike-air-max-2019-green-uk-shoeso">nike air max 2019 green uk</a> <a href="http://www.yufnsyb.com/nike-air-max-tailwind-8-bl%C3%A5-tooth-kurpesc">nike air max tailwind 8 bl氓 tooth</a>
    georginabauer http://www.georginabauer.com/

  6. avatar moveinplace

    <a href="http://www.cmetechnology.com/air-jordan-vii-7-retro-b%C3%B8rn-scarpea">air jordan vii 7 retro b酶rn</a> <a href="http://www.hepseninle.com/nike-free-flyknit-5.0-blu-ink-kurpesf">nike free flyknit 5.0 blu ink</a> <a href="http://www.mecazadam.com/nike-mens-kyle-emanuel-limited-black-jersey-los-angeles-chargers-nfl-51-2016-salute-to-service-nflr">nike mens kyle emanuel limited black jersey los angeles chargers nfl 51 2016 salute to service</a> <a href="http://www.medfarmailac.com/nike-kobe-9-gold-crimson-schwarz-kurpesf">nike kobe 9 gold crimson schwarz</a> <a href="http://www.mileikanea.com/femminile-nike-air-force-1-gituttio-rosso-schuhef">femminile nike air force 1 gituttio rosso</a> <a href="http://www.paolamoney.com/adidas-ultra-boost-core-noir-jack-obuvg">adidas ultra boost core noir jack</a>
    moveinplace http://www.moveinplace.com/

  7. avatar suchmeldungen

    <a href="http://www.swnblog.com/nike-free-rn-red-up-sneakere">nike free rn red up</a> <a href="http://www.weimpactmds.com/nike-flyknit-racer-lagoon-blau-xm%C3%A4nner-kurpesa">nike flyknit racer lagoon blau xm盲nner</a> <a href="http://www.relsflorida.com/kd-8-green-new-sneakerb">kd 8 green new</a> <a href="http://www.votuongluan.com/nike-lebron-13-low-team-red-line-shoesg">nike lebron 13 low team red line</a> <a href="http://www.hanayaflowers.com/chicago-blackhawks-bucket-hat-hats">chicago blackhawks bucket hat</a> <a href="http://www.jlchatlaw.com/nike-air-max-90-vt-pink-blue-sneakerf">nike air max 90 vt pink blue</a>
    suchmeldungen http://www.suchmeldungen.com/

  8. avatar boobseze

    <a href="http://www.acforlessllc.com/red-yellow-womens-nike-air-max-1-essential-shoes-runninge">red yellow womens nike air max 1 essential shoes</a> <a href="http://www.thehodostore.com/converse-chuck-taylor-gore-tex-hi-duck-camo-zapatosu">converse chuck taylor gore tex hi duck camo</a> <a href="http://www.gandoff.com/nike-flyknit-racer-legion-bl%C3%A5-springs-chaussuresa">nike flyknit racer legion bl氓 springs</a> <a href="http://www.langbenlang.com/limited-robbie-gould-jersey-chicago-bears-9-camo-fashion-black-nfle">limited robbie gould jersey chicago bears 9 camo fashion black</a> <a href="http://www.glennloovens.com/air-max-plus-white-tiger-runningg">air max plus white tiger</a> <a href="http://www.kamilotomotiv.com/zdeno-chara-33-boston-bruins-reebok-premier-alternate-jersey-small-nflv">zdeno chara 33 boston bruins reebok premier alternate jersey small</a>
    boobseze http://www.boobseze.com/

  9. avatar ycbrowsbeauty

    <a href="http://www.cakeeaterspgh.com/nike-roshe-run-speckle-orange-grey-shoesc">nike roshe run speckle orange grey</a> <a href="http://www.leesgoedplus.com/billig-nike-zoom-pegasus-32-kvinders-bl%C3%A5-skog">billig nike zoom pegasus 32 kvinders bl氓</a> <a href="http://www.lieupekings.com/yellow-purple-womens-nike-air-force-1-high-shoes-sportsf">yellow purple womens nike air force 1 high shoes</a> <a href="http://www.marieebeaute.com/nike-air-max-tailwind-5-orange-dream-runningd">nike air max tailwind 5 orange dream</a> <a href="http://www.dentaleden.com/limited-jason-hanson-jersey-detroit-lions-4-camo-fashion-black-nflg">limited jason hanson jersey detroit lions 4 camo fashion black</a> <a href="http://www.jellyville.com/nike-flyknit-lunar-3-silver-black-skoa">nike flyknit lunar 3 silver black</a>
    [url=http://www.ycbrowsbeauty.com/]ycbrowsbeauty[/url]

  10. avatar allenchute

    <a href="http://www.momandpopop.com/hermes-alligator-bracelet-hermesr">hermes alligator bracelet</a> <a href="http://www.artlaurey.com/adidas-nmd-runner-vapour-pink-light-onix-exclusive-zero-shoesv">adidas nmd runner vapour pink light onix exclusive zero</a> <a href="http://www.ochapan.com/coach-tote-crossgrain-leather-coacha">coach tote crossgrain leather</a> <a href="http://www.brooklyngay.com/asics-gel-kinsei-2-dam%C3%A4nner-schwarz-shoesa">asics gel kinsei 2 dam盲nner schwarz</a> <a href="http://www.royphelps.com/nike-kyrie-2-purple-skos">nike kyrie 2 purple</a> <a href="http://www.agneaublanc.com/burberry-belt-price-singapore-beltr">burberry belt price singapore</a>
    allenchute http://www.allenchute.com/

  11. avatar PDM系统

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

  12. avatar 宁波PLM

    这文章值得学习一下

  13. avatar PDM系统

    这么好的文章值得学习

  14. avatar 盘古科技

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

  15. avatar 老董优惠码

    不错 感谢分享

  16. avatar 轿车托运公司

    好东西 点个赞

欢迎留言




用户登录

sitemap