본문 바로가기

IT/iOs

생체인증 구현하기

iOS 생체인증 구현하기

 

아래 소스 코드는 각 기기에 맞게 터치아이디 또는 페이스아이디를 사용한 생체 인증을 구현한 코드입니다.

 

 

Objective-C

@property (strong, nonatomic) LAContext * laContext;


- (BOOL)isFaceIdSupported {
    static BOOL ret = NO;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
#if TARGET_IPHONE_SIMULATOR
        NSString *model = NSProcessInfo.processInfo.environment[@"SIMULATOR_MODEL_IDENTIFIER"];
#else
        struct utsname systemInfo;
        uname(&systemInfo);
        NSString *model = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
#endif
        ret = [model isEqualToString:@"iPhone10,3"] || [model isEqualToString:@"iPhone10,6"] || [model isEqualToString:@"iPhone11,2"]
        || [model isEqualToString:@"iPhone11,4"] || [model isEqualToString:@"iPhone11,6"] || [model isEqualToString:@"iPhone11,8"]
        || [model isEqualToString:@"iPhone12,1"] || [model isEqualToString:@"iPhone12,3"] || [model isEqualToString:@"iPhone12,5"];
    });
    return ret;
}

- (void)biometricsUIShow {
    if (_laContext != nil) {
        [_laContext invalidate];
        _laContext = nil;
    }
    TASetting *setting = [TASetting sharedInstance];
    _laContext = [[LAContext alloc] init];
    NSError *authError = nil;
    NSString *myLocalizedReasonString = @"지문을 입력해 주세요.";
    if ( [TAUtil isFaceIdSupported] ) {
        myLocalizedReasonString = @"얼굴을 입력해 주세요.";
    }
    
    if ([_laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
        _laContext.localizedFallbackTitle = @"";
        
        [_laContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                   localizedReason:myLocalizedReasonString
                             reply:^(BOOL succes, NSError *error) {
                                 
                                 if (succes) {
                                     dispatch_async(dispatch_get_main_queue(), ^{
                                     });
                                 } else {
                                     dispatch_async(dispatch_get_main_queue(), ^{
                                         switch (error.code) {
                                             case LAErrorAuthenticationFailed:
                                             {
												//@“터치ID 등록에 실패했습니다."
                                                 //@“페이스ID 등록에 실패했습니다."
                                             }
                                                 break;
                                                 
                                             case LAErrorUserCancel:
                                             {
                                                 //
                                             }
                                                 break;
                                                 
                                             case LAErrorUserFallback:
                                                 break;
                                             case LAErrorBiometryLockout:
                                             {
                                                 //
                                             }
                                                 break;
                                             default:
                                             {
                                                 //
                                             }
                                                 break;
                                         }
                                     });
                                     
                                 }
                             }];
    } else {
        //@“터치ID 설정을 확인하세요.”
		//@"페이스ID 설정을 확인하세요."
    }
}

 

swift

    public func faceIDAvailable() -> Bool {
        var result = false
        if #available(iOS 11.0, *) {
            let context = LAContext()
            result = (context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthentication, error: nil) && context.biometryType == .faceID)
        }
        if IS_X {
            result = true
        }
        
        return result
    }        
        
        
    let context = LAContext()
    context.localizedFallbackTitle = ""

    // cancel button title
    if #available(iOS 10.0, *) {
        context.localizedCancelTitle = ""
    }

     context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: faceIDAvailable() ? kFaceIdAuthenticationReason : kTouchIdAuthenticationReason) { (success, error) in
        DispatchQueue.main.async {
            if success {  }
            else {
                switch Int32(error.errorCode) {
                case kLAErrorAuthenticationFailed:
                    //failed
                case kLAErrorUserCancel:
                    //canceledByUser
                case kLAErrorUserFallback:
                    //fallback
                case kLAErrorSystemCancel:
                    //canceledBySystem
                case kLAErrorPasscodeNotSet:
                    //passcodeNotSet
                case kLAErrorBiometryNotAvailable:
                    //biometryNotAvailable
                case kLAErrorBiometryNotEnrolled:
                    //biometryNotEnrolled
                case kLAErrorBiometryLockout:
                    //biometryLockedout
                default:
                   return other
                }
            }
        }
    }