我的应用程序中的登录系统出现问题。
当应用程序第一次打开时,FVC 是主视图控制器。FVC 然后检查我是否已登录/我的会话密钥是否仍然有效,如果没有,则它会使 LoginViewController 在我的整个屏幕上弹出,迫使我登录以继续。一旦我使用正确的用户名和密码登录,它会快速检查网络上的 JSON 文件,如果它没有返回错误,它会返回一个会话密钥。问题是,我知道它正确地获取 JSON 文件并解析它,因为我使用 NSLog 进行了一些测试,但是一旦我使用正确的信息登录,它就会关闭 loginView 半秒钟,然后显示主视图,然后loginView 立即弹出!有什么不对,我希望你能找到我的代码的问题。迈克尔。
第一个视图控制器:
- (void)viewDidAppear:(BOOL)animated
{
//Put login check here.
LoginViewController *login = [self.storyboard instantiateViewControllerWithIdentifier:@"login"];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// create the URL we'd like to query
[[NSURLCache sharedURLCache] removeAllCachedResponses];
myURL = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@%@", @"https://URL/v1/?get&action=getservers&session_key=", login.sessionKey]];
// we'll receive raw data so we'll create an NSData Object with it
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
// now we'll parse our data using NSJSONSerialization
id myJSON = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:nil];
// typecast an array and list its contents
NSDictionary *jsonArray = (NSDictionary *)myJSON;
NSLog(@"%@",[jsonArray objectForKey:@"status"]);
if ([[jsonArray objectForKey:@"status"] isEqualToString:@"ERROR"]) {
[self presentViewController:login animated:NO completion:nil];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
登录视图控制器:
- (IBAction)loginAction:(id)sender {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// create the URL we'd like to query
NSURL *myURL = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@%@%@%@", @"https://URL/v1/?get&action=login&username=", usernameField.text, @"&password=", passwordField.text]];
// we'll receive raw data so we'll create an NSData Object with it
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
// now we'll parse our data using NSJSONSerialization
id myJSON = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:nil];
// typecast an array and list its contents
//NSArray *jsonArray = (NSArray *)myJSON;
NSDictionary *jsonArray = (NSDictionary *)myJSON;
NSLog(@"%@",[jsonArray objectForKey:@"status"]);
if ([[jsonArray objectForKey:@"status"] isEqualToString:@"OK"]) {
FirstViewController *dashView = [self.storyboard instantiateViewControllerWithIdentifier:@"dashView"];
sessionKey = [jsonArray objectForKey:@"new_session_key"];
NSLog(@"%@",sessionKey);
[self dismissViewControllerAnimated:YES completion:nil];
} else {
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}