IPhone 5S 부터 지문등록을 통한 생체 인증이 가능한데 앱에 터치 아이디 인증을 구현 하는 코드를 정리해보자.



storyboard의 뷰에 버튼 오브젝트를 등록하여 터치 아이디 인증을 구현하는 예제이다. 

(LocalAuthentication 을 import 해야한다.)


@IBAction func testTouchID(sender: AnyObject) {

        let context = LAContext()

        var error : NSError?

        

        if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {

            //디바이스가 터치아이디를 이용할수 있음.

            context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "Access requires authentication", reply: {(success,error) in dispatch_async(dispatch_get_main_queue()) {

                

                    if error != nil { // 터치아이디로 정상적인 Access 안되었을

                                        // error type 따라 메시징 처리

                        switch error!.code {

                            case LAError.SystemCancel.rawValue :

                                self.notifyUser("Session cancelled", err: error?.localizedDescription)

                            case LAError.UserCancel.rawValue :

                                self.notifyUser("Please try again", err: error?.localizedDescription)

                            case LAError.UserFallback.rawValue :

                                self.notifyUser("Authentication", err: "Password option selected")

                            default :

                                self.notifyUser("Authentication failed", err: error?.localizedDescription)

                        }

                    } else {    // 정상적인 Access 되었을 메시징 처리

                        self.notifyUser("Authentication Successful", err: "You now have full access")

                    }

                }

            })

        }else {

            //디바이스가 터치아이디를 이용할수 없음.

            switch error!.code {

            case LAError.TouchIDNotEnrolled.rawValue :

                notifyUser("TouchID is not enrolled", err: error?.localizedDescription)

            case LAError.PasscodeNotSet.rawValue :

                notifyUser("A passcode has not been set", err: error?.localizedDescription)

            default :

                notifyUser("TouchID not available", err: error?.localizedDescription)

                

            }

        }

    }

    

    func notifyUser(msg: String, err:String?) {

        let alert = UIAlertController(title: msg, message: err, preferredStyle: UIAlertControllerStyle.Alert)

        let cancelAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)

        alert.addAction(cancelAction)

        self.presentViewController(alert, animated: true, completion: nil)

    }

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }





위의 
코드는  "핵심만 골라 배우는 ios 9 프로그래밍 (닐스미스 지음/ 황반석 옮김)" 에 있는 예제를 참고하여 작성하였습니다.

+ Recent posts