앱 내 Touch ID를 사용한 사용자 인증
소개
검증 환경
사용하는 기능
LocalAuthentication 프레임워크
LAContext 클래스
방법
설명
canEvaluatePolicy(_: error:) -> Bool
Touch ID가 탑재되어 있어 유효한 상태, 또는 Passcode 가 설정되어 있는 상태 (Touch ID 가 없어도)이면 true 를 돌려준다.
evaluatePolicy(_: localizedReason: reply:)
Touch ID 또는 Passcode를 사용하여 인증을 수행합니다. 인증 결과에 대한 처리는 제 3 인수의 콜백으로서 지정된다.
LAPolicy 열거형
상수
설명
deviceOwnerAuthenticationWithBiometrics
iOS 8에서 이용. iOS 9 이상에서 이 상수를 이용하면 폴백 버튼(지문이 인증되지 않은 경우 Passcode 입력을 촉구하는 버튼)을 눌러도 입력 화면이 나오지 않습니까? 1
deviceOwnerAuthentication
iOS 9 이상에서 사용.
구현 예
ViewController.swiftimport UIKit
import LocalAuthentication
class ViewController: UIViewController {
@IBOutlet weak var authResultLabel: UILabel!
@IBAction func authenticate(_ sender: UIButton) {
let myContext = LAContext()
self.configure(context: myContext)
let reason = "Only device owner can use this feature."
var authError: NSError? = nil
// Touch ID が有効または Passcode がセットされている状態であることを確認
if myContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {
// 認証を実行
myContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { (success, evaluateError) in
if success {
print("Success")
DispatchQueue.main.async {
self.authResultLabel.text = "Success"
}
} else {
let error = evaluateError! as NSError
let errorMessage = "\(error.code): \(error.localizedDescription)"
print(errorMessage)
DispatchQueue.main.async {
self.authResultLabel.text = errorMessage
}
}
}
} else {
// Touch ID と Passcode のいずれも有効ではない場合
let errorMessage = "\(authError!.code): \(authError!.localizedDescription)"
print(errorMessage)
DispatchQueue.main.async {
self.authResultLabel.text = errorMessage
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
/// ボタンの文字列をカスタマイズ
private func configure(context: LAContext) {
context.localizedCancelTitle = "キャンセル"
context.localizedFallbackTitle = "パスコードを入力"
}
}
실행 결과
초기 상태(Authenticate 버튼을 누르면 지문 인증 실행)
지문 인증
지문 인증 실패(패스 코드 입력을 촉구하는 버튼 표시)
패스코드 입력
성공시
취소 시
패스코드가 설정되지 않은 경우
샘플 프로젝트
import UIKit
import LocalAuthentication
class ViewController: UIViewController {
@IBOutlet weak var authResultLabel: UILabel!
@IBAction func authenticate(_ sender: UIButton) {
let myContext = LAContext()
self.configure(context: myContext)
let reason = "Only device owner can use this feature."
var authError: NSError? = nil
// Touch ID が有効または Passcode がセットされている状態であることを確認
if myContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {
// 認証を実行
myContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { (success, evaluateError) in
if success {
print("Success")
DispatchQueue.main.async {
self.authResultLabel.text = "Success"
}
} else {
let error = evaluateError! as NSError
let errorMessage = "\(error.code): \(error.localizedDescription)"
print(errorMessage)
DispatchQueue.main.async {
self.authResultLabel.text = errorMessage
}
}
}
} else {
// Touch ID と Passcode のいずれも有効ではない場合
let errorMessage = "\(authError!.code): \(authError!.localizedDescription)"
print(errorMessage)
DispatchQueue.main.async {
self.authResultLabel.text = errorMessage
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
/// ボタンの文字列をカスタマイズ
private func configure(context: LAContext) {
context.localizedCancelTitle = "キャンセル"
context.localizedFallbackTitle = "パスコードを入力"
}
}
초기 상태(Authenticate 버튼을 누르면 지문 인증 실행)
지문 인증
지문 인증 실패(패스 코드 입력을 촉구하는 버튼 표시)
패스코드 입력
성공시
취소 시
패스코드가 설정되지 않은 경우
샘플 프로젝트
참고 사이트
App-Level Passcode | Cocoanetics
LAContext - LocalAuthentication | Apple Developer Documentation
Apple 공식 문서에서 이 동작에 대한 설명을 찾을 수 없습니다. 아시는 분이 계시면, 지적 받을 수 있으면 기쁩니다. 또한 iOS 8이 장착된 기기가 없었기 때문에 iOS 8에서 확인할 수 없습니다. ↩
Reference
이 문제에 관하여(앱 내 Touch ID를 사용한 사용자 인증), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/aokiplayer/items/a43d1b302ea7fba40970텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)