[IOS] The problem that the Head setting in the Alamofire library in iOS7 is invalid is that alamofireios7

Source: Internet
Author: User

[IOS] The problem that the Head setting in the Alamofire library in iOS7 is invalid is that alamofireios7


StatementWelcome to repost, but please keep the original source of the article :) blog Park: http://www.cnblogs.com farmer UNCLE: http://over140.cnblogs.com

 

Body

There is no problem with the same code in iOS8, and iOS7 won't be able to get the data (it will include errors such as Cocoa Error 3840). The tracing finds that the request Head parameter settings are useless, according to the reference at the bottom of the text, the compatible code is changed:

Private func getRequest (method: Method, _ URLString: URLStringConvertible, parameters: [String: AnyObject]? = Nil)-> NSMutableURLRequest {
Let request = NSMutableURLRequest (URL: NSURL (string: URLString. URLString )!)
Request. HTTPMethod = method. rawValue
If parameters! = Nil {
Request. HTTPBody = NSJSONSerialization. dataWithJSONObject (parameters !, Options: nil, error: nil)
}
Request. setValue (API_UA, forHTTPHeaderField: "User-Agent ")
Request. setValue (HEADER_ACCEPT, forHTTPHeaderField: "Accept ")
Request. setValue ("application/json", forHTTPHeaderField: "Content-Type ")

Return request
}

Here, API_UA and HEADER_ACCEPT can be changed to their own Head parameters, using the code:

Var request: Request!
If DeviceUtils. isIOS7 (){
Request = mHttpManager. request (getRequest (method, URLString, parameters: parameters ))
} Else {
Request = mHttpManager. request (method, URLString, parameters: parameters, encoding: ParameterEncoding. JSON)
}
// Request. responseJSON ....

Code Description:

A) mHttpManager is Manager. sharedInstance. Do not forget to set mHttpManager. session. configuration. HTTPAdditionalHeaders. It works in iOS8.

 

Refer:

Setting Custom HTTP Headers in Alamofire in iOS 7 not working

(Refer to the post and try to set NSURLSessionConfiguration but it does not work)

 

End

Continue iOS, continue swift, and solve problems.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.