Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无限登录循环?_Ios_Objective C_Json_Login - Fatal编程技术网

Ios 无限登录循环?

Ios 无限登录循环?,ios,objective-c,json,login,Ios,Objective C,Json,Login,我的应用程序中的登录系统出现问题 当应用程序首次打开时,FVC是主视图控制器。FVC然后检查我是否登录/我的会话密钥是否仍然有效,如果无效,它会使LoginViewController在我的整个屏幕上弹出,迫使我继续登录。一旦我使用正确的用户名和密码登录,它会快速检查web上的JSON文件,如果没有返回错误,它会返回会话密钥。问题是,我知道它正确地获取了JSON文件并对其进行了解析,就像我使用NSLog进行了一些测试一样,但只要我使用正确的信息登录,它就会关闭loginView,并在半秒钟内显示

我的应用程序中的登录系统出现问题

当应用程序首次打开时,FVC是主视图控制器。FVC然后检查我是否登录/我的会话密钥是否仍然有效,如果无效,它会使LoginViewController在我的整个屏幕上弹出,迫使我继续登录。一旦我使用正确的用户名和密码登录,它会快速检查web上的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;

}
登录视图控制器:

 - (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;

}

我认为问题在于登录.sessionKey。不要登录它。可能是零。我看不出你把它放在哪里了。这可能是因为您的Web服务出现了错误。查看它。

登录并看到FVC后,nslog显示nslog(@“%@,[jsonArray objectForKey:@“status”])@Yan,它显示:“2013-08-08 16:49:04.297 AHMC[3690:907]OK 2013-08-08 16:49:04.298 AHMC[3690:907]b891945bb5344180a2ef25db07c770”我怀疑这是问题,但我没有看到[super ViewDidDisplay:动画]在[self-presentViewController:login animated:NO completion:nil];然后查看是否在登录后调用它这就是问题所在。您可能应该将登录信息存储在某个全局变量或nsuserdefaults中,这样您就不必每次出现此视图时都调用Web服务。我猜这取决于您的程序