Adding user information to a request

Since it is ASIHTTPRequest

deprecated, I am migrating my code to use server communication NSURLSession

. I am currently using the NSDictionary

"userInfo" property ASIHTTPRequest

to send additional information about the user. Description of "userInfo" in the ASIHTTPRequest

documentation: "User information related to the request (not sent to the server)". After processing the request, I re- fetch this "userInfo" object from the request object and take the appropriate action. My example code ASIHTTPRequest

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 the same functionality via NSURLSession, how can I do this?

Sample NSURLSession code:

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

      

+3


source to share


2 answers


Since you are using a completion handler to use the block variable, try the code below:



    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


source


The cleanest way is to subclass NSURLRequest

and add the desired property - be it a tag or userInfo and use it just like you are used to the ASIHTTP

framework.



0


source







All Articles