简体   繁体   English

iOS登录segue修复

[英]iOS login segue fix

This is the success parameter to my login which is under the closing bracket in viewDidload 这是我的登录成功参数,它位于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"]];
    }
}];

}

And this is the programatic connection in the view that I want to show when the user is authorized 这是我要在授权用户时显示的视图中的程序化连接

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

The problem is when I enter saved logged in credentials in my database (ie username: user1 password: password) I get the correct UIAlert I'm supposed to receive (Logged in - Welcome user1). 问题是,当我在数据库中输入保存的登录凭据(即用户名:user1密码:password)时,我得到了我应该收到的正确UIAlert(登录-欢迎use​​r1)。 But when i enter (username:user1 password: fake password) I still get the logged in UIAlert. 但是当我输入(用户名:user1密码:假密码)时,我仍然得到登录的UIAlert。 Also if i try to register a new user from the app ie (username:user2 password: password 2) I get an authentication failed UIAlert. 另外,如果我尝试从该应用程序注册一个新用户,即(username:user2 password:password 2),我将收到身份验证失败的UIAlert。 Also my segue doesn't actually happen when I enter valid credentials. 另外,当我输入有效的凭据时,我的查询实际上并没有发生。 Any tips? 有小费吗?

Updated: logout delegate 更新:注销代表

-(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"]];
    }
}];

Find the lines with inline comments //Added and try those in your code. 找到带有内联注释//Added的行,然后在代码中尝试。 Also as mentioned update login view -> viewWillAppear and logout method. 还如前所述,更新登录视图-> viewWillAppear和注销方法。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM