koogawa blog

iOS、Android、foursquareに関する話題

iOS 8で導入されたLocal Authenticationを使ってTouchID認証

iOS 8で導入されたLocal Authenticationを使ってTouchID認証 - Qiita Qiitaでも紹介しております。


iOS 8から追加された Local Authentication でTouchID認証を行うドキュメントが 一般にも公開 されていたのでメモ。

Local Authentication Framework Reference

サンプルコード

- (void)authStart
{
    // 認証用のオブジェクト生成
    LAContext *myContext = [[LAContext alloc] init];
    NSError *authError = nil;
    NSString *myLocalizedReasonString = @"認証を行う理由をここに書く";
    
    // 生体認証(TouchID)の利用が可能か?
    if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics 
                                              error:&authError])
    {
        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:myLocalizedReasonString
                            reply:^(BOOL success, NSError *error)
         {
             if (success) {
                 // 認証に成功した
             }
             else {
                 // 認証に失敗した
             }
         }];
    }
    else {
        // TouchIDが使用できない(古い端末を使用した場合など)
    }
}

evaluatePolicy には認証ポリシーを指定できますが、現時点では LAPolicyDeviceOwnerAuthenticationWithBiometrics のみ指定可能なようです。

注意点

LocalAuthenticationは実機でのみ動作するようです。シミュレータで実行しようとするとエラーになります。

Xcode 6は現在NDA下にあるため、実行結果のスクリーンショットの掲載は控えておきます。