簡體   English   中英

iOS登錄segue修復

[英]iOS login segue fix

這是我的登錄成功參數,它位於viewDidload中的右括號下

//check whether it's a login or register
NSString* command = (sender.tag==1)?@"register":@"login";
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, @"command", fldUsername.text, @"username", hashedPassword, @"password", nil];
//make the call to the web API
[[API sharedInstance] commandWithParams:params onCompletion:^(NSDictionary *json) {
    //result returned
    NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0];
    if ([json objectForKey:@"error"]==nil && [[res objectForKey:@"IdUser"] intValue]>0) {
        [[API sharedInstance] setUser: res];
        [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
        //show message to the user
        [[[UIAlertView alloc] initWithTitle:@"Logged in" message:[NSString stringWithFormat:@"Welcome %@",[res objectForKey:@"username"]] delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil] show];
    } else {
        //error
        [UIAlertView error:[json objectForKey:@"error"]];
    }
}];

}

這是我要在授權用戶時顯示的視圖中的程序化連接

-(void)viewDidload
[super viewDidLoad];
{
if (![[API sharedInstance] isAuthorized]) {
    [self performSegueWithIdentifier:@"ShowLogin" sender:nil];
}

問題是,當我在數據庫中輸入保存的登錄憑據(即用戶名:user1密碼:password)時,我得到了我應該收到的正確UIAlert(登錄-歡迎use​​r1)。 但是當我輸入(用戶名:user1密碼:假密碼)時,我仍然得到登錄的UIAlert。 另外,如果我嘗試從該應用程序注冊一個新用戶,即(username:user2 password:password 2),我將收到身份驗證失敗的UIAlert。 另外,當我輸入有效的憑據時,我的查詢實際上並沒有發生。 有小費嗎?

更新:注銷代表

-(void)logout {
//logout the user from the server, and also upon success destroy the local authorization
[[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"logout", @"command", nil] onCompletion:^(NSDictionary *json) {
   //logged out from server
   [API sharedInstance].user = nil;
   [self performSegueWithIdentifier:@"ShowLogin" sender:nil];
}];
}
[[API sharedInstance] commandWithParams:params onCompletion:^(NSDictionary *json) {
    //result returned
    NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0];
    if ([json objectForKey:@"error"]==nil && [[res objectForKey:@"IdUser"] intValue]>0) {
        [[API sharedInstance] setUser: res];
        [API sharedInstance].isAuthorized = YES; //Added
        [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
        //show message to the user
        [[[UIAlertView alloc] initWithTitle:@"Logged in" message:[NSString stringWithFormat:@"Welcome %@",[res objectForKey:@"username"]] delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil] show];
    } else {
        [[API sharedInstance] setUser: nil]; //Added
        [API sharedInstance].isAuthorized = NO; //Added
        //error
        [UIAlertView error:[json objectForKey:@"error"]];
    }
}];

找到帶有內聯注釋//Added的行,然后在代碼中嘗試。 還如前所述,更新登錄視圖-> viewWillAppear和注銷方法。

希望這可以幫助。

暫無
暫無

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

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