Add user information in request

669 views Asked by At

Since ASIHTTPRequest is deprecated, I am migrating my code to use NSURLSession based server communication. Currently, I am using NSDictionary "userInfo" property of ASIHTTPRequest to send additional user information. The description of "userInfo" in ASIHTTPRequest documentation is "Custom user information associated with the request (not sent to the server)". After the request is processed, I re-fetch this "userInfo" object from the request object and take action accordingly. My ASIHTTPRequest code example is

Request:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:@"http://www.google.com"];
[request setDelegate:self];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:@"requestCount"];
[request setUserInfo:userInfo];

I want to achieve same functionality through NSURLSession, how can I do this?

NSURLSession code example:

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: self.queue];

NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:timeOutSeconds];

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);

                                                           }
                                                       }

                                                   }];

[dataTask resume];
2

There are 2 answers

0
Dilip Lilaramani On BEST ANSWER

Because you are using completion handler so you can use block variable, try below code:

    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: self.queue];

    NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:timeOutSeconds];

    __block NSDictionary *userInfo = [NSDictionary dictionaryWithObjectAndKeys...........];**

    NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                                   completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                       NSLog(@"Response:%@ %@\n", response, error);

                                                       NSLog(@"UserInfo: %@", userInfo);
                                                       if(error == nil)
                                                       {
                                                           NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                                           NSLog(@"Data = %@",text);

                                                           }
                                                       }

                                                   }];

    [dataTask resume];
0
Michal On

The cleanest way to go is to subclass NSURLRequest and add the desired property - be it tag, or userInfo and use it the same way you are used to with ASIHTTP framework.