在每个View Controller中显示用户名jsondata

时间:2015-11-20 02:02:01

标签: ios objective-c json global-variables

  1. 我目前正在使用Objective-C开发IOS应用程序。我遇到的问题是,我需要在每个视图控制器中显示jsondata的用户名​​。 (比如facebook,当你登录后,你的全名/用户名会显示在上面)

  2. 我使用Github中的sbjson v3.1.1框架在我的应用程序中执行用户身份验证。如何从jsondata调用用户名数据? (我的jsondata数组中有1,userid,密码和用户名)

  3. 3.这是我的验证码。

    @try {
    
        if([[txtLogin text] isEqualToString:@""] || [[txtPassword text] isEqualToString:@""] ) {
            [self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
        } else {
            NSString *post =[[NSString alloc] initWithFormat:@"userid=%@&pass=%@",[txtLogin text],[txtPassword text]];
            NSLog(@"PostData: %@",post);
    
            NSURL *url=[NSURL URLWithString:@"http://192.168.1.14:8888/itrack/mobile/login.php?"];
    
            NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
            NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:url];
            [request setHTTPMethod:@"POST"];
            [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:postData];
    
            //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
    
            NSError *error = [[NSError alloc] init];
            NSHTTPURLResponse *response = nil;
            NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
            NSLog(@"Response code: %d", [response statusCode]);
            if ([response statusCode] >=200 && [response statusCode] <300)
            {
                NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
                NSLog(@"Response ==> %@", responseData);
    
                SBJsonParser *jsonParser = [SBJsonParser new];
                NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
                NSLog(@"%@",jsonData);
                NSInteger success = [(NSNumber *) [jsonData objectForKey:@"login"] integerValue];
                NSLog(@"%d",success);
    
              //  NSString *test = (NSString *) [jsonData objectForKey:@"username"];
    
    
                if(success == 1)
                {
                    NSLog(@"Login SUCCESS");
                    [self alertStatus:@"Logged in Successfully." :@"Login Success!"];
    
                    homepageVC *controller = [self.storyboard   instantiateViewControllerWithIdentifier:@"homeEN"];
                    [self presentViewController:controller animated:YES completion:nil];
    
                    //redirect to homepage
                   // UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"main" bundle:nil];
                    //UIViewController *newView = [mainStoryboard instantiateViewControllerWithIdentifier:@"login"];
                    //[self presentViewController:newView animated:YES completion:nil];
    
    
                } else {
    
                    NSString *error_msg = (NSString *) [jsonData objectForKey:@"err"];
                    [self alertStatus:error_msg :@"Login Failed!"];
                }
    
            } else {
                if (error) NSLog(@"Error: %@", error);
                [self alertStatus:@"Connection Failed" :@"Login Failed!"];
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
        [self alertStatus:@"Login Failed." :@"Login Failed!"];
    }
    }
    

1 个答案:

答案 0 :(得分:1)

其中一种方法是在NSUserDefaults中存储用户名,然后在每个其他视图控制器中检索。这样,如果用户名密钥不是nil,那么你知道用户确实进行了身份验证。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:"TheName" forKey:@"username"];
// you can store any other properties if neeeded
[defaults synchronize]; // to save the data into NSUserDefaults

在任何其他视图控制器中检索用户名,即使应用程序已重新启动,您也可以

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 NSString *userName = [defaults objectForKey:@"userName"];

如果您的情况有效,请告诉我。