[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.