Je travaille sur une application qui nécessiterait une authentification Touch ID, est-il donc possible d'utiliser Touch ID (scanner d'empreintes digitales) dans le simulateur?
Veuillez également partager une sorte d'exemple de code pour l'utilisation du framework LocalAuthentication.
Depuis la dernière version bêta (6), il n'y a aucun moyen de simuler un balayage d'empreintes digitales sur le simulateur. Pour être honnête, je doute que cela soit inclus même dans les versions ultérieures.
Vous devrez tester sur l'appareil.
Pour utiliser le cadre d'authentification en ce moment, vous avez besoin de: * XCode 6 * iPhone 5s avec iOS 8
Les étapes que vous devez effectuer sont les suivantes:
Vérifiez si l'appareil prend en charge la validation des empreintes digitales et si une empreinte digitale est enregistrée:
@import LocalAuthentication;
// Get the local authentication context:
LAContext *context = [[LAContext alloc] init];
// Test if fingerprint authentication is available on the device and a fingerprint has been enrolled.
if ([context canEvaluatePolicy: LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil])
{
NSLog(@"Fingerprint authentication available.");
}
Validez une empreinte digitale uniquement:
[context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:@"Authenticate for server login" reply:^(BOOL success, NSError *authenticationError){
if (success) {
NSLog(@"Fingerprint validated.");
}
else {
NSLog(@"Fingerprint validation failed: %@.", authenticationError.localizedDescription);
}
}];
Validez une empreinte digitale ou le code d'accès de l'appareil en fonction du choix de l'utilisateur: Cela dépasse un peu la portée d'une question ici, veuillez trouver plus d'informations sur: https://www.secsign.com/fingerprint-validation-as-an-alternative-to-passcodes/
XCODE 7 beta prend en charge le test de l'authentification Touch ID dans iPhone Simulator.Vous pouvez essayer ceci pour vos tests.
[Capture d'écran 1]
[Capture d'écran 2]
Dans l'objectif C
@import LocalAuthentication;
@interface EnterPasscodeVC ()
-(void)viewWillAppear:(BOOL)animated {
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = @"Authentication is required to access your QPay Apps.";
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL success, NSError *error) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"Success" sender:nil];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:error.description
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alertView show];
switch (error.code) {
case LAErrorAuthenticationFailed:
NSLog(@"Authentication Failed");
// Rather than show a UIAlert here, use the error to determine if you should Push to a keypad for PIN entry.
break;
case LAErrorUserCancel:
NSLog(@"User pressed Cancel button");
break;
case LAErrorUserFallback:
NSLog(@"User pressed \"Enter Password\"");
break;
default:
NSLog(@"Touch ID is not configured");
break;
}
NSLog(@"Authentication Fails");
});
}
}];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:authError.description
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alertView show];
// Rather than show a UIAlert here, use the error to determine if you should Push to a keypad for PIN entry.
});
}
}
En Swift
import LocalAuthentication
override func viewDidLoad() {
super.viewDidLoad()
authenticateUser()
}
// MARK: Method implementation
func authenticateUser() {
// Get the local authentication context.
let context = LAContext()
// Declare a NSError variable.
var error: NSError?
// Set the reason string that will appear on the authentication alert.
let reasonString = "Authentication is needed to access your notes."
// Check if the device can evaluate the policy.
if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error) {
[context .evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success: Bool, evalPolicyError: NSError?) -> Void in
if success {
// If authentication was successful then load the data.
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.loadData()
})
}
else{
// If authentication failed then show a message to the console with a short description.
// In case that the error is a user fallback, then show the password alert view.
print(evalPolicyError?.localizedDescription)
switch evalPolicyError!.code {
case LAError.SystemCancel.rawValue:
print("Authentication was cancelled by the system")
case LAError.UserCancel.rawValue:
print("Authentication was cancelled by the user")
case LAError.UserFallback.rawValue:
print("User selected to enter custom password")
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.showPasswordAlert()
})
default:
print("Authentication failed")
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.showPasswordAlert()
})
}
}
})]
}
else{
// If the security policy cannot be evaluated then show a short message depending on the error.
switch error!.code{
case LAError.TouchIDNotEnrolled.rawValue:
print("TouchID is not enrolled")
case LAError.PasscodeNotSet.rawValue:
print("A passcode has not been set")
default:
// The LAError.TouchIDNotAvailable case.
print("TouchID not available")
}
// Optionally the error description can be displayed on the console.
print(error?.localizedDescription)
// Show the custom alert view to allow users to enter the password.
showPasswordAlert()
}
}
func showPasswordAlert() {
let passwordAlert : UIAlertView = UIAlertView(title: "TouchIDDemo", message: "Please type your password", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Okay")
passwordAlert.alertViewStyle = UIAlertViewStyle.SecureTextInput
passwordAlert.show()
}
func loadData(){
if appDelegate.checkIfDataFileExists() {
self.dataArray = NSMutableArray(contentsOfFile: appDelegate.getPathOfDataFile())
self.tblNotes.reloadData()
}
else{
print("File does not exist")
}
}
// MARK: UIAlertViewDelegate method implementation
func alertView(alertView: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {
if buttonIndex == 1 {
if !alertView.textFieldAtIndex(0)!.text!.isEmpty {
if alertView.textFieldAtIndex(0)!.text == "appcoda" {
loadData()
}
else{
showPasswordAlert()
}
}
else{
showPasswordAlert()
}
}
}