ios – 未设置NSURLSessionConfiguration HTTPAdditionalHeaders

前端之家收集整理的这篇文章主要介绍了ios – 未设置NSURLSessionConfiguration HTTPAdditionalHeaders前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
授权标头在NSURLSessionConfiguration中设置,但它未附加到NSURLSessionDataTask.这是Foundation框架中的错误吗?
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
[configuration setHTTPAdditionalHeaders:@{@"Authorization":@"123"}];

// Initialize session with NSURLSessionConfiguration
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSURLSessionDataTask *sessionTask = [session dataTaskWithRequest:request
                                        completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
}];

[sessionTask resume];

解决方法

我在Swift中尝试这个并且它可以工作
var sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
    var xHTTPAdditionalHeaders: [NSObject : AnyObject] = ["X-test":"taly"]
    sessionConfig.HTTPAdditionalHeaders = xHTTPAdditionalHeaders
    let session = NSURLSession(configuration: sessionConfig)

    let task = session.dataTaskWithURL(url,completionHandler: {data,response,error -> Void in
原文链接:https://www.f2er.com/iOS/328399.html

猜你在找的iOS相关文章