小编ele*_*nag的帖子

具有令牌认证的NSURLSession

我在我的iOS项目中有以下代码,我想转换为使用NSURLSession而不是NSURLConnection.我正在查询REST API哪个使用基于令牌的HTTP Authentication方案,但我找不到如何做的示例.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];

NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"Username"];

NSString *token = //GET THE TOKEN FROM THE KEYCHAIN


NSString *authValue = [NSString stringWithFormat:@"Token %@",token];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];


if ([NSURLConnection canHandleRequest:request]){
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    [NSURLConnection sendAsynchronousRequest:request queue:self.fetchQueue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

                               if (!connectionError) {
                                   NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                                   if (httpResponse.statusCode == 200){
                                       NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];

                                       //Process the …
Run Code Online (Sandbox Code Playgroud)

objective-c ios ios7 nsurlsession

10
推荐指数
1
解决办法
1万
查看次数

标签 统计

ios ×1

ios7 ×1

nsurlsession ×1

objective-c ×1