Hi i am getting the response from my server successfully.i need to access the user_id send by the server in my app.
check my code:
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"my url"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params=[[NSString alloc]initWithFormat:@"mobile=%@",[self.reqnum text ]];
NSLog(@"%@",params);
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
[[NSUserDefaults standardUserDefaults]setObject:@"Y" forKey:@"login"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}];
[dataTask resume];
for this code i am getting the response like:
here i need to access the user_id in my app .so can i get that particular user_id.
Thank You.
Since original solutions have already been posted, I will focus on longer & more tedious way which I think is the proper way to handle the elephant in the room. This will help you in the longer run.
Create a Singleton class since there can be only one user logged in at one time.
SharedUser.h
SharedUser.m
Convert your response into
NSDictionary
.Populate your sharedInstance with the result attributes:
Now your user's attributes will be available anywhere in the App. You just need to import
SharedUser.h
to desiredUIView
,UIViewController
& type following to access your data:Also Note that I am using singleton pattern because I am assuming that you only need to handle one user's attributes which will be used in multiple viewcontrollers over the span of time. If you need multiple users saved, create a similar user model class and populate them in a similar way. (Just don't make them singleton).
Also I would suggest that you should read Ray Wenderlich's series tutorials on:
1. Object Oriented Design
2. Design Patterns
3. Intro to iOS design patterns
Yes, its upto you how to store data and if you want to parse it, then try
Parse the data that you're getting in block as below.
The response is a JSON object. If what you are asking is how to parse it, then there is an inbuilt JSON parser in iOS.
Create a NSDictionary to get json data.
You can make a NSDictionary, save JSON data in it and fetch user_id from it. Like this: