我已将 Touch id 集成到我的应用程序中,该过程开始如下 -
1.当用户启动应用程序时,将调用 Authenticaion.m 中的 View 方法,然后它要求用户使用触摸 ID 进行身份验证,并显示一个带有“输入密码”和“取消”的警报对话框。
2.如果这是用户第一次登录,他将选择使用手指进行身份验证,然后将显示 Login.m ,其中有用户名和密码文本字段,他必须输入他的凭据才能登录。
3.他也可以选择“输入密码”或“取消”,这将带他进入login.m,在那里他将输入他的用户名和密码进行身份验证。
4.如果用户登录应用程序并从后台状态退出应用程序并尝试登录应用程序,那么由于他没有注销应用程序,他将看到相同的视图将出现方法,如果他使用身份验证他的手指触摸id,他不需要输入他的用户名和密码进行身份验证,他直接登录,现在另一种情况是如果他再次选择“输入密码”或按“取消”,他将被带到login.m 文件,他必须在其中输入用户名和密码。
5.如果用户使用上述任何一种方式登录应用程序并退出应用程序并再次尝试登录,则重复步骤1的所有过程。我想实现上述功能。
我的疑问是,在 LAContext 的成功块中,我需要如何存储 nsUserDefaults 的 txtUsername 和 txtPassword 以便应用程序识别出用户没有从应用程序中注销,并且一旦他从后台删除应用程序并再次尝试登录并使用他的手指,他将直接登录到应用程序。请参阅我在 authentication.m 和 login.m 中使用的以下代码。请让我知道如何解决这个问题,因为我上周一直在努力解决这个问题。
-(void)viewWillAppear:(BOOL)animated{
dispatch_queue_t highPriorityQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.75 * NSEC_PER_SEC), highPriorityQueue, ^{
LAContext *myContext = [[LAContext alloc] init];
NSError *authError = nil;
NSString *myLocalizedReasonString = @"Please Authenticate To Proceed";
if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&authError]) {
[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
localizedReason:myLocalizedReasonString
reply:^(BOOL success, NSError *error) {
if (success) {
NSString *result = (NSString *) [[NSUserDefaults standardUserDefaults] objectForKey:@"txtUserName"];
SignInViewController *user = [[SignInViewController alloc]init];
if ([result isEqualToString:@"rthottempudi" ] ) {
NSLog(@"User is authenticated successfully");
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
@"Main" bundle:[NSBundle mainBundle]];
ViewController *congoView = [storyboard instantiateViewControllerWithIdentifier:@"CongratsViewController"];
[self presentViewController:congoView animated:YES completion:nil];
});
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Authentication Failed"
message:@"PLease Try Again"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alertView show];
NSLog(@"failed");
});
}
}
else {
switch (error.code) {
case LAErrorAuthenticationFailed:
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Authentication Failed"
message:@"Please Try Again"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alertView show];
NSLog(@"Authentication Failed");
break;
}
case LAErrorUserCancel:
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
@"Main" bundle:[NSBundle mainBundle]];
SignInViewController *congoView = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
[self presentViewController:congoView animated:YES completion:nil];
NSLog(@"User pressed Cancel button");
break;
}
case LAErrorUserFallback:
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
@"Main" bundle:[NSBundle mainBundle]];
SignInViewController *congoView = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
[self presentViewController:congoView animated:YES completion:nil];
NSLog(@"User pressed \"Enter Password\"");
break;
}
default:
NSLog(@"Touch ID is not configured");
break;
}
NSLog(@"Authentication Fails");
}
}];
}
else {
// if the device doesn't have touch id
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
@"Main" bundle:[NSBundle mainBundle]];
SignInViewController *congoView = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
[self presentViewController:congoView animated:YES completion:nil];
});
}
});
}