2015-02-23 28 views
0

我已經集成觸摸ID到我的應用程序和過程如下開始 -如何使用Touch ID和NSUserDefaults?

1.當用戶啓動應用程序,查看將出現在Authenticaion.m方法將被調用,然後它要求用戶可通過觸摸來進行身份驗證ID並顯示一個帶有「輸入密碼」的提示對話框&「取消」。

2.如果這是用戶第一次登錄,他將選擇使用他的手指進行身份驗證,然後登錄.m將顯示在用戶名爲&的密碼文本字段中,他必須輸入其憑據才能登錄。

3.他也可以選擇「輸入密碼」或「取消」,這將把他帶到login.m,他將輸入他的用戶名和密碼進行身份驗證。

4.如果用戶登錄到應用程序並從後臺狀態退出應用程序並嘗試登錄到應用程序,那麼因爲他沒有註銷應用程序,他將顯示相同的視圖將出現方法如果他用手指進行身份驗證,他不需要輸入他的用戶名和密碼進行身份驗證,並且他直接登錄,現在另一種情況是如果他再次選擇「輸入密碼」或按「取消」他將被帶到login.m文件,他必須輸入用戶名和密碼。

5.如果用戶使用上述任何一種方式登錄到應用程序並退出應用程序並再次嘗試登錄,則重複步驟1中的所有過程。我想實現上述功能。

我的DOUBT是在LAContext的成功塊中,我如何需要爲txtUsername存儲NSUserDefaults & 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]; 



    }); 


} 

    }); 
} 

回答

2

私人數據不存儲在UserDefaults,

而不是在鑰匙串店的用戶名和密碼,存儲TouchIDEnabled在UserDefaults值(真/假)。

如果TouchIdEnabled爲true,請詢問touchId。

如果touchID成功,請從鑰匙串中讀取用戶名密碼。