iOS 開發學習之路(登陸驗證調用WebService)二,ioswebservice

來源:互聯網
上載者:User

iOS 開發學習之路(登陸驗證調用WebService)二,ioswebservice

    swift3.0下使用Alamofire調用Webservice遇到的一些問題以及解決方案。

    首先是針對沒有認證的https下的介面處理問題(ps:不推薦在正式版本中使用),manager.request替換掉了Alamofire.request。

let manager = Alamofire.SessionManager.default        manager.delegate.sessionDidReceiveChallenge = { session, challenge in            var disposition: URLSession.AuthChallengeDisposition = .performDefaultHandling            var credential: URLCredential?                        if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {                disposition = URLSession.AuthChallengeDisposition.useCredential                credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)            } else {                    if challenge.previousFailureCount > 0 {                        disposition = .cancelAuthenticationChallenge                    } else {                        credential = manager.session.configuration.urlCredentialStorage?.defaultCredential(for: challenge.protectionSpace)                        if credential != nil {                        disposition = .useCredential                    }                }            }                        return (disposition, credential)        }

    針對soap協議,使用mutableURLRequest來進行請求。

        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)

    得到返回的值包含來xml命名空間和其中的有效結果。通過第三方庫SWXMLHash來進行XML的解析,再針對解析得到的Json字串利用JSONSerialization獲得相應的字典。 

        manager.request(mutableURLRequest as URLRequest).responseData{ response in            //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:帳號或密碼有誤!"{                let data = a.data(using: String.Encoding.utf8)! as Data                let a_dic = try? JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableContainers)                if let dict = a_dic as? Dictionary<String, String> {                    print(dict["姓名"]!)                    print(dict["地區"]!)                    callback?(true)                }            }else{                callback?(false)            }        }

    注意上面使用了一個回呼函數,這是因為Alamofire調用WebService是非同步方式,這裡通過isOk來判定登陸是否成功。

 func reqWebService(values: Dictionary<String, String>, callback: ((_ isOk: Bool)->Void)?){}

        

        

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.