简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM