繁体   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