前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 开发之路(登陆验证调用WebService)二

iOS 开发之路(登陆验证调用WebService)二

作者头像
从今若
发布2019-09-19 15:45:59
8030
发布2019-09-19 15:45:59
举报
文章被收录于专栏:家劲家劲家劲

    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)?){}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-11-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档