问题描述
您好我的服务器响应成功了。我需要访问我的应用中服务器发送的user_id。
Hi i am getting the response from my server successfully.i need to access the user_id send by the server in my app.
检查我的代码:
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];
此代码的响应如下:
在这里我需要访问我的应用程序中的user_id。我可以获得特定的user_id。
here i need to access the user_id in my app .so can i get that particular user_id.
谢谢。
推荐答案
由于原始解决方案已经发布,我将重点关注更长时间和更长时间我认为更加繁琐的方式是在房间里处理大象的正确方法。从长远来看,这将对您有所帮助。
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.
创建一个Singleton类,因为一次只能有一个用户登录。
SharedUser.h
#import <Foundation/Foundation.h>
@interface SharedUser : NSObject
@property (strong, nonatomic) NSString* userId;
@property (strong, nonatomic) NSString* userName;
@property (strong, nonatomic) NSString* subscriptionStatus;
@property (strong, nonatomic) NSString* registerDate;
@property (strong, nonatomic) NSString* expiryDate;
+(SharedUser*) getInstance;
@end
SharedUser.m
#import "SharedUser.h"
@implementation SharedUser
static SharedUser * sharedInstance;
+(SharedUser*) getInstance
{
@synchronized(self)
{
if(sharedInstance == nil)
{
sharedInstance = [[SharedUser alloc] init];
sharedInstance.userName = @"";
sharedInstance.userId = @"";
sharedInstance.subscriptionStatus = @"";
sharedInstance.registerDate = @"";
sharedInstance.expiryDate = @"";
return sharedInstance;
}
else
{
return sharedInstance;
}
}
}
将您的回复转换为 NSDictionary
。
Convert your response into NSDictionary
.
NSDictionary *json_dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];;//From Santosh Reddy's Answer
使用结果属性填充您的sharedInstance:
[SharedUser getInstance].userId = [json_dict objectForKey:@"user_id"];
[SharedUser getInstance].userName = [json_dict objectForKey:@"username"];
[SharedUser getInstance].subscriptionStatus = [json_dict objectForKey:@"subscription_status"];
[SharedUser getInstance].registryDate = [json_dict objectForKey:@"register_date"];//Better to use NSDate type instead of NSString
[SharedUser getInstance].expiryDate = [json_dict objectForKey:@"expiry_date"];
现在,您的用户属性将在应用程序的任何位置可用。您只需要将 SharedUser.h
导入所需的 UIView
, UIViewController
&键入以下内容以访问您的数据:
Now your user's attributes will be available anywhere in the App. You just need to import SharedUser.h
to desired UIView
, UIViewController
& type following to access your data:
NSString *userId = [SharedUser getInstance].userId;
另请注意我使用的是单例模式,因为我假设您只需要处理一个用户的属性这将在一段时间内用于多个视图控制器。如果需要保存多个用户,请创建一个类似的用户模型类,并以类似的方式填充它们。 (只是不要让他们单身)。
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).
另外我建议您阅读Ray Wenderlich的系列教程:
Also I would suggest that you should read Ray Wenderlich's series tutorials on:
这篇关于如何保存响应来自我的服务器,以及如何访问该数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!