在iOS 7中的Alamofire中设置自定义HTTP标头不起作用 [英] Setting Custom HTTP Headers in Alamofire in iOS 7 not working

查看:449
本文介绍了在iOS 7中的Alamofire中设置自定义HTTP标头不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在iOS 7中使用我的自定义HTTP标头设置 Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ,但我没有运气。

I've tried to set the Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders with my custom HTTP Headers in iOS 7 but I have had no luck.

这在iOS 8中运行良好。

This works fine in iOS 8.

有没有人有任何建议?

推荐答案

我搞定了。

这对iOS7没有影响:

This has no effect on iOS7:

Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["CustomHeader": customValue]

但这对两者都适用iOS7& 8:

This however will work on both iOS7 & 8:

var headers = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
    headers["CustomHeader"] = customValue

    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.HTTPAdditionalHeaders = headers

    Alamofire
        .Manager(configuration: configuration)
        .request(.GET, "https://example.com/api/users", parameters: nil, encoding: .JSON)

这篇关于在iOS 7中的Alamofire中设置自定义HTTP标头不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆