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