簡體   English   中英

如何檢查我的用戶是否已經登錄(會話)並轉到特定的ViewController?

[英]How can I check if my user is already logged in (session), and go to specific ViewController?

我正在使用Drupal iOS SDK將用戶登錄到我的應用程序。 效果很好。 也就是說,有時我的用戶不會“注銷”,而只是關閉應用程序。 當他們這樣做時,會話不會過期; 因此,當他們重新啟動應用程序並嘗試通過輸入用戶名和密碼登錄時,他們就不能這樣做,因為Drupal告訴他們他們已經登錄。

如果會話仍然有效,我將如何寫一行代碼以在加載登錄視圖時檢查該會話,該代碼基本上指出“如果用戶已經登錄,請轉到[ViewController的名稱在這里]”?

我認為它看起來像這樣:

        - (void)viewDidLoad {
            [super viewDidLoad];

            DIOSSession *session = [DIOSSession sharedSession];
            //If you have logged in, this object is not nil
          [session user];

            if ([session user]!= nil) {

// User login displayed

            } else {

     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
     MyAccountViewController *yourViewController = (MyAccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"];
     [self.navigationController pushViewController:yourViewController animated:YES];


            }

我的登錄代碼當前如下所示:

ViewController.m

- (IBAction)loginButton:(UIButton *)sender {
    self.activityIndicatorViewOne.hidden = NO;
    [self.activityIndicatorViewOne startAnimating];


    [DIOSUser
     userLoginWithUsername:_userField.text
     andPassword:_passField.text
     success:^(AFHTTPRequestOperation *op, id response) {

         wrongLogin.hidden = YES;

         UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
         MyAccountViewController *yourViewController = (MyAccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"];
         [self.navigationController pushViewController:yourViewController animated:YES];

         [self.activityIndicatorViewOne stopAnimating];
         self.activityIndicatorViewOne.hidden = YES;

         NSLog(@"Success!");}

     failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"Fail!"); wrongLogin.hidden = NO; }
     ];

}

登出方法

-(void)flipView {

    [DIOSUser
     userLogoutWithSuccessBlock:^(AFHTTPRequestOperation *op, id response) {  NSLog(@"Success!");

         //Pop back to the root view controller
         [self.navigationController popToRootViewControllerAnimated:NO];

         //Allocate and init the new view controller to push to
         ViewController *newVC = [[ViewController alloc] init];

         //Push the new view controller
         [self.navigationController pushViewController:newVC animated:YES];

     }
     failure:^(AFHTTPRequestOperation *op, NSError *err) {  NSLog(@"Fail!"); }
     ];


}

關閉/打開應用程序后,您將沒有會話,因為SDK不會以某種方式存儲用戶會話和CSRF令牌,即使關閉后它也仍會保留在手機中。 在iOS中,您幾乎沒有辦法。 您擁有NSUserDefaults是最快的方法,但這有點不安全。 然后,您將擁有Keychain ,這將是這里的最佳選擇(安全&用於會話和密碼之類的東西)-但實現起來並不容易。 另一個選項可能是“ 核心數據”,但對於這個小任務來說太大了。 我將向您展示如何在NSUserDefaults中做到這一點 ,因為我不知道您如何存儲鑰匙串項目(如果不存儲,我建議您查看UICKeychainStore )。

因此,這里的想法是在用戶登錄時保存會話和令牌。然后在用戶退出時清除會話和令牌。 然后,當您的用戶進入登錄頁面時,可以確定是否有會話,可以向他顯示我的帳戶視圖控制器。

可能是這樣(未經測試):

登錄:

[DIOSUser userLoginWithUsername:_userField.text
                    andPassword:_passField.text
                        success:^(AFHTTPRequestOperation *op, id response) {
     // Saving to keychain/NSUserDefaults
     [[NSUserDefaults standardUserDefaults] setObject:[[DIOSSession sharedSession] user]
                                               forKey:@"diosSession"];
     [[NSUserDefaults standardUserDefaults] synchronize];
     [[DIOSSession sharedSession] getCSRFTokenWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSString *csrfToken = [NSString stringWithUTF8String:[responseObject bytes]];
         [[NSUserDefaults standardUserDefaults] setObject:csrfToken forKey:@"diosToken"];
         [[NSUserDefaults standardUserDefaults] synchronize];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         // failure handler
     }];

     wrongLogin.hidden = YES;
     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
     MyAccountViewController *yourViewController = (MyAccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"];
     [self.navigationController pushViewController:yourViewController animated:YES];

     [self.activityIndicatorViewOne stopAnimating];
     self.activityIndicatorViewOne.hidden = YES;

     NSLog(@"Success!");}

                        failure:^(AFHTTPRequestOperation *op, NSError *err) { NSLog(@"Fail!"); wrongLogin.hidden = NO; }
 ];

登出:

[DIOSUser userLogoutWithSuccessBlock:^(AFHTTPRequestOperation *op, id response) {  NSLog(@"Success!");
     // Remove the keys
     [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"diosSession"];
     [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"diosToken"];
     [[NSUserDefaults standardUserDefaults] synchronize];

     //Pop back to the root view controller
     [self.navigationController popToRootViewControllerAnimated:NO];

     //Allocate and init the new view controller to push to
     ViewController *newVC = [[ViewController alloc] init];

     //Push the new view controller
     [self.navigationController pushViewController:newVC animated:YES];

 }
 failure:^(AFHTTPRequestOperation *op, NSError *err) {  NSLog(@"Fail!"); }
 ];

檢查用戶是否已登錄:

NSDictionary *user = [[NSUserDefaults standardUserDefaults] objectForKey:@"diosSession"];
NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"diosToken"];
if (user && token) {
    // Logged in, set the old session & token
    [[DIOSSession sharedSession] setUser:user];
    [[DIOSSession sharedSession] setCsrfToken:token];

    // Push view controller
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MyAccountViewController *yourViewController = (MyAccountViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MyAccount"];
    [self.navigationController pushViewController:yourViewController animated:YES];
} else {
    // Not logged in, show form
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM