Karan Alangat
Karan Alangat

Reputation: 2224

How to add token to a Rest web service request in iOS Objective C

I have a rest web service request to be called in Objective C. How to add token to web service request for authentication ??

Thanks in advance

Upvotes: 0

Views: 516

Answers (1)

VRAwesome
VRAwesome

Reputation: 4803

Add token in request as below

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:yourURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"GET"]; // Set your method
[request addValue:@"token_value" forHTTPHeaderField:@"Authorization"];
// ...... your code
// ........ add data

NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        //HideProcess;
        if (error)
        {
            //NSLog(@"Error : %@\n", error);
            return;
        }

        if (data != nil)
        {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
            NSLog(@"Response :\n%@\n", dict);
        }
    }];
    [task resume];

Upvotes: 1

Related Questions