IOS Development Learning Path (login verification call WebService) II

Source: Internet
Author: User

SWIFT3.0 uses Alamofire to invoke some of the problems encountered by WebService and the solution.

The first is to address the issue of interface processing under HTTPS without certificates (PS: Not recommended for use in the official version), Manager.request replaced Alamofire.request.

Let manager = Alamofire.sessionmanager.defaultManager.Delegate. Sessiondidreceivechallenge = {session, challengeinch            varDisposition:URLSession.AuthChallengeDisposition =. performdefaulthandlingvarCredential:urlcredential?ifChallenge.protectionSpace.authenticationMethod = =Nsurlauthenticationmethodservertrust {Disposition=URLSession.AuthChallengeDisposition.useCredential Credential= Urlcredential (trust:challenge.protectionspace.servertrust!)            } Else {                    ifChallenge.previousfailurecount >0{Disposition=. Cancelauthenticationchallenge}Else{credential= Manager.session.configuration.urlCredentialStorage?. Defaultcredential ( for: Challenge.protectionspace)ifCredential! =Nil {disposition=. Usecredential}} }                        return(disposition, credential)}

For the SOAP protocol, use Mutableurlrequest to make the request.

Mutableurlrequest.setvalue ("text/xml; Charset=utf-8", Forhttpheaderfield:"Content-type") Mutableurlrequest.setvalue (Action, Forhttpheaderfield:"SOAPAction") Mutableurlrequest.setvalue (String (SoapMsg.characters.count), Forhttpheaderfield:"Content-length") Mutableurlrequest.httpmethod="POST"Mutableurlrequest.httpbody= Soapmsg.data (using: String.Encoding.utf8)

Gets the returned value that contains the XML namespace and the valid results therein. Through the third-party library Swxmlhash to parse the XML, and then to parse the resulting JSON string using jsonserialization to obtain the corresponding dictionary.

Manager.request (mutableurlrequest asurlrequest). responsedata{responseinch            //DebugPrint (response)Let XML = Swxmlhash.parse (response.data!) Let A:string= (xml["Soap:envelope"]["Soap:body"]["Findusernewresponse"]["Findusernewresult"].element?. Text)!print (a)if!a.isempty && A! ="ERR: Incorrect account or password! "{Let data= A.data (using: String.Encoding.utf8)! asData let A_dic=Try? Jsonserialization.jsonobject (With:data asData, Options:JSONSerialization.ReadingOptions.mutableContainers)ifLet dict = A_dic as? Dictionary<string, string>{print (dict["name"]!) print (dict["Area"]!) Callback? (true)                }            }Else{callback? (false)            }        }

Note that a callback function is used, because the Alamofire call WebService is asynchronous, and this is done by isOK to determine whether the login was successful.

Func Reqwebservice (values:dictionary<string, String>, Callback: ((_ Isok:bool)->void)?) {}

        

        

IOS Development Learning Path (login verification call WebService) II

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.