Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.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 删除UIViewController'中的所有用户数据;注销后发生什么_Ios_Objective C_Cocoa - Fatal编程技术网

Ios 删除UIViewController'中的所有用户数据;注销后发生什么

Ios 删除UIViewController'中的所有用户数据;注销后发生什么,ios,objective-c,cocoa,Ios,Objective C,Cocoa,当我登录到我的应用程序时,我的应用程序确实会推送ViewControllerXYZMainViewController,XYZMainViewController视图将出现:动画的调用方法,该方法向我的API发出请求以检索经过身份验证的用户数据,此时我会更新标签文本以显示用户名。当我注销应用程序时,它会将我返回到登录ViewController,当我与其他用户再次登录时,XYZMainViewController标签文本包含前一个用户的名称,而不更新标签文本 XYZMainViewControl

当我登录到我的应用程序时,我的应用程序确实会推送ViewController
XYZMainViewController
XYZMainViewController
视图将出现:动画的调用方法,该方法向我的API发出请求以检索经过身份验证的用户数据,此时我会更新标签文本以显示用户名。当我注销应用程序时,它会将我返回到登录ViewController,当我与其他用户再次登录时,XYZMainViewController标签文本包含前一个用户的名称,而不更新标签文本

XYZMainViewController.m

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self homeProfile];
}

- (void)homeProfile
{
    [NXOAuth2Request performMethod:@"GET"
                        onResource:[NSURL URLWithString:@"http://{url}/users/userinfo"]
                   usingParameters:nil
                       withAccount:[XYZCommonFunctions user]
               sendProgressHandler:nil
               responseHandler:^(NSURLResponse *response, NSData *responseData, NSError *error){
                   NSDictionary *parsedData = [[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error] objectForKey:@"data"];
                   _user = [parsedData objectForKey:@"user"];
                   [self.label setText:[NSString stringWithFormat:@"Welcome %@!", [_user objectForKey:@"username"]]];
               }];

}

- (IBAction)logout:(id)sender {
    XYZAppDelegate* appDelegate = (XYZAppDelegate*)[[UIApplication sharedApplication] delegate];
    [appDelegate logout];
}
- (void)login
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *identifier = [prefs stringForKey:@"accountidentifier"];
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    NSString *viewIdentifier = @"WelcomeView";
    if(identifier != nil){
        NXOAuth2Account *account = [[NXOAuth2AccountStore sharedStore] accountWithIdentifier:identifier];
        if(account != nil) {
            viewIdentifier = @"MainView";
        }
        UIViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier: viewIdentifier];
        [navigationController pushViewController:controller animated:NO];
        return;
    }
}

- (void)logout
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs removeObjectForKey:@"accountidentifier"];
    [prefs synchronize];
    for (NXOAuth2Account *a in [[NXOAuth2AccountStore sharedStore] accounts] ){
        [[NXOAuth2AccountStore sharedStore] removeAccount:a];
    }
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    [navigationController popToRootViewControllerAnimated:YES];
}
XYZAppDelegate.m

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self homeProfile];
}

- (void)homeProfile
{
    [NXOAuth2Request performMethod:@"GET"
                        onResource:[NSURL URLWithString:@"http://{url}/users/userinfo"]
                   usingParameters:nil
                       withAccount:[XYZCommonFunctions user]
               sendProgressHandler:nil
               responseHandler:^(NSURLResponse *response, NSData *responseData, NSError *error){
                   NSDictionary *parsedData = [[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error] objectForKey:@"data"];
                   _user = [parsedData objectForKey:@"user"];
                   [self.label setText:[NSString stringWithFormat:@"Welcome %@!", [_user objectForKey:@"username"]]];
               }];

}

- (IBAction)logout:(id)sender {
    XYZAppDelegate* appDelegate = (XYZAppDelegate*)[[UIApplication sharedApplication] delegate];
    [appDelegate logout];
}
- (void)login
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *identifier = [prefs stringForKey:@"accountidentifier"];
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
    NSString *viewIdentifier = @"WelcomeView";
    if(identifier != nil){
        NXOAuth2Account *account = [[NXOAuth2AccountStore sharedStore] accountWithIdentifier:identifier];
        if(account != nil) {
            viewIdentifier = @"MainView";
        }
        UIViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier: viewIdentifier];
        [navigationController pushViewController:controller animated:NO];
        return;
    }
}

- (void)logout
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs removeObjectForKey:@"accountidentifier"];
    [prefs synchronize];
    for (NXOAuth2Account *a in [[NXOAuth2AccountStore sharedStore] accounts] ){
        [[NXOAuth2AccountStore sharedStore] removeAccount:a];
    }
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    [navigationController popToRootViewControllerAnimated:YES];
}
我需要重新初始化
XYZMainViewController
中的所有数据


谢谢。

看起来问题与获取JSON对象有关。每次您都可能发送相同的用户以获取用户数据。您不是使用NSUserdefault对象来显示名称,而是使用值,该值由JSON对象返回。据我所知,错误的原因是“withAccount:[XYZCommonFunctions user]”行。

我建议使用,而不是使用

-(void)viewWillAppear:(BOOL)animated {
你可以用

- (void)viewDidLoad
这样,您的登录操作仅在LoginController加载时执行,而不是在LoginController出现时执行

新视图将显示如下所示-

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self.label setText:@""];
}
和ViewDidLoad-

- (void)viewDidLoad 
{
  [super viewDidLoad];
  [self homeProfile];
}
还要检查您的json响应,无论您得到的是响应成功还是错误。根据响应需要进行处理


希望这有帮助

“withAccount:[XYZCommonFunctions user]”行只是检索OAuth access_令牌以向API发出请求的方法。NSUserDefaults仅用于查找用于发出这些请求的帐户的ID。谢谢你,nadim。你好,vicky,这不起作用,我已经试过了,但还是像以前一样,现在我按照你说的做了,我处理请求错误。谢谢。用户成功登录后,在进入下一个视图(VC)之前,应清除标签(用户名)