Resolves the problem where the Alamofire library sets the head invalid under IOS7 _ios

Source: Internet
Author: User

The same code under IOS8 No problem, iOS7 can not get the data (will be packet cocoa error 3840 errors), tracking Discovery request head parameter settings regardless of use, according to the text at the bottom of the reference to change the compatibility code:

Code
  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 change to their 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, pay attention don't forget to set mHttpManager.session.configuration.HTTPAdditionalHeaders, under IOS8 still works

Reference:

Setting Custom HTTP Headers in Alamofire with IOS 7 not working (reference post, also try setting nsurlsessionconfiguration but no effect)

The above is the Alamofire library under the iOS set head data collation, hoping to help develop the iOS software students.

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.