如何使用Swift使用TouchID?

use*_*085 5 objective-c ios swift ios8

Apple为iOS 8的TouchID实现提供的文档在Objective-C中.

有Swift版本吗?

Objective-C的:

- (IBAction)touchIDAvailable:(UIButton *)touchIDAvailableButton {
    LAContext *context = [[LAContext alloc] init];
    __block  NSString *msg;
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:NSLocalizedString(@"Place your finger on the sensor", nil) reply: ^(BOOL success, NSError *authenticationError) {
         if (success) {
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

迅速:

@IBAction func touchidbutton(sender: AnyObject) {
    authContext.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "Place your finger on the sensor"?, reply: ((success : Bool, NSError!) -> Void)?){
        if (success) {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

mac*_*ome 6

这是我的视图控制器在Swift中执行这些检查.在研究这个时,我发现Swift中的完成块/闭包语法非常混乱.

请注意,Beta 2中的某些选项已更改,以便您可以更好地控制Touch ID对话框,例如禁用后备选项或取消按钮.

// Imports
import UIKit
import LocalAuthentication

// Class Implementation
class FirstViewController: UIViewController {

// Class Properties
@IBOutlet var statusLabel : UILabel
@IBOutlet var headerString: UILabel
var authError : NSError?
var authContext = LAContext()
var statusText = ""
var alert = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.Alert)


// Class Methods
@IBAction func swiftButtonPress(sender : AnyObject) {

    statusLabel.text = "Authenticating"

    //Can we use local auth?
    if authContext.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authError) {

        authContext.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics,
            localizedReason: "I need to see if it's really you",
            reply: {(success: Bool, error: NSError!) -> Void in

                if success {
                    self.statusText = "Touch ID success!"
                    self.alert.title = "Success"
                    self.alert.message = "I knew it was you!"
                } else {
                    self.statusText = "Touch ID failed!"
                    self.alert.title = "Failure"

                    switch error!.code {
                    case LAError.AuthenticationFailed.toRaw():
                        self.alert.message = "Authentication Failed"
                    case LAError.UserCancel.toRaw():
                        self.alert.message = "User canceled!"
                    case LAError.SystemCancel.toRaw():
                        self.alert.message = "The system canceled!"
                    case LAError.UserFallback.toRaw():
                        self.alert.message = "User request to enter passcode"
                    default:
                        self.alert.message = "Something else went wrong"
                    }
                }
                self.presentViewController(self.alert, animated: true, completion:{self.statusLabel.text = self.statusText})
            })
    } else {
        self.statusText = "No local authentication"
        alert.title = "Uh oh!"

        switch authError!.code {
        case LAError.TouchIDNotAvailable.toRaw():
            alert.message = "No Touch ID on device"
        case LAError.TouchIDNotEnrolled.toRaw():
            alert.message = "No fingers enrolled"
        case LAError.PasscodeNotSet.toRaw():
            alert.message = "No passcode set"
        default:
            alert.message = "Something went wrong getting local auth"
        }
        self.presentViewController(self.alert, animated: true, completion: {self.statusLabel.text = self.statusText})
    }
    resetTouchID()
}

// Reset the system so we can go again
func resetTouchID() {
    authContext = LAContext()
    alert = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil))
    let passcodeDetector = SwiftPasscodeDetector()
    if passcodeDetector.checkForPasscode() {
        headerString.text = "Passcode Set on Device"
    } else {
        headerString.text = "No Passcode Set"
    }

}

// Inherited Methods
override func viewDidLoad() {
    super.viewDidLoad()
    resetTouchID()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
Run Code Online (Sandbox Code Playgroud)

}


use*_*085 1

找到了!

下面的链接来自 Github 上一位名为 Shmoopi 的用户。Shmoopi 制作该应用程序来测试用 Swift 编程的 TouchID。

https://github.com/Shmoopi/Swift-Touch-ID