How to save the response From My server , and How

2020-07-20 03:49发布

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.

7条回答
爷、活的狠高调
2楼-- · 2020-07-20 04:42

If you want to use the value in other classes then :

First create a data Model Class, parse the data dictionary/JSON and store it. Using the completion block you can return the specific/received user_id to the caller.

Here you are getting in JSON, you can parse it and get the desired data:

NSDictionary *respDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSString *userID = respDict[@"user_id"];
查看更多
登录 后发表回答