iOS 开发之路(登陆验证调用WebService)二
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 > {
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)?){}
iOS 开发之路(登陆验证调用WebService)二的更多相关文章
- iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)
本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文<iOS ...
- iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)
转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html 一. 概况 本文接着 iOS 开 ...
- ios开发中全局变量设置和调用方法
ios开发中,全局变量设置和调用方法如下:在AppDelegate.h文件中设置全局变量:@interface ***AppDelegate{NSString *myName;}@property ( ...
- ios开发:如何用js调用ios
本文转载至 :http://blog.chinaunix.net/uid-29415710-id-4058564.html - (BOOL)webView:(UIWebView *)webView s ...
- iOS开发之使用XMPPFramework实现即时通信(二)
上篇的博客iOS开发之使用XMPPFramework实现即时通信(一)只是本篇的引子,本篇博客就给之前的微信加上即时通讯的功能,主要是对XMPPFramework的使用.本篇博客中用到了Spark做测 ...
- iOS 开发之路(登陆页键盘遮挡输入框问题)一
在学习开发登陆页的时候,遇到的问题分享如下: 首先是swift 3.0 中,NotificationCenter 设置 selector 如下: @IBOutlet weak var bottomCo ...
- iOS 开发之路(WKWebView内嵌HTML5之图片上传) 五
HTML5页面的图片上传功能在iOS端的实现. 首先,页面上用的是plupload组件,在wkwebview上存在两个坑需要修复才能正常使用. 问题:在webview上点击选择照片/相机拍摄,就会出现 ...
- iOS 开发之路(使用WKWebView加载Html5) 四
基于Swift 3 . Xcode 8 . iOS 10 下的WKWebView的使用. 首先是WKWebView的基本用法: var wk:WKWebView! var progBar:UIProg ...
- iOS 开发之路(AES/DES加密实现) 三
最近接触的这个项目由于以前服务器上用的是DES/CBC/PKCS5Padding加密方式,为了让在iOS上的加密结果与服务器端保持一致,我做了很多尝试,现在分享给大家.PS:现在不推荐用DES了,只是 ...
随机推荐
- MVC中Action参数绑定的过程
一.题外话 上一篇:MVC中Action的执行过程 ControllerContext 封装有了与指定的 RouteBase 和 ControllerBase 实例匹配的 HTTP 请求的信息. 二. ...
- 在Visual Studio 2015 Preview 中使用Github 版本控制
打开Visual Studio,新建项目,右下角勾选,如下图: 点击‘OK’后,出现下图窗口,选择'Git' : 如果是现有项目可以在‘文件’菜单下找到‘Add to Source Control’ ...
- StgCreateDocfileOnILockBytes复合文档
CRichEditCtrl 的ole技术 ------------ IRichEditOle --------------------------- 如需向CRichEditCtrl里面插入Ole对象 ...
- C#+ html 实现类似QQ聊天界面的气泡效果
/**定义两个人的头像*/ Myhead = "<img src=qrc:/chatdemo/Msg/Head.png width='30px'heigth='30px'>&qu ...
- 【转】Key/Value之王Memcached初探:一、掀起Memcached的盖头来
一.Memcached是何方神圣? 在数据驱动的Web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的HttpRuntim ...
- 钉钉开发中post异步调用问题
最近项目上在做钉钉开发中,经常会遇到使用post方式调用钉钉内部的方法(微信也有一样),这里涉及到跨域的post调用,但跨域一般都是用jsonp格式,而这个格式只支持get方式.尝试了挺多方法都没有返 ...
- Redis系列一之数据结构
一.Redis简介 redis是一个高性能的key-value非关系数据库,它可以存键(key)与5种不同类型的值(value)之间的映射(mapping),支持存储的value类型包括:String ...
- 【Java每日一题】20161027
package Oct2016; public class Ques1027 { public static void main(String[] args) { System.out.println ...
- Saas
SaaS是Software-as-a-Service(软件即服务)的简称,随着互联网技术的发展和应用软件的成熟, 在21世纪开始兴起的一种完全创新的软件应用模式.它与“on-demand softwa ...
- Scalaz(38)- Free :Coproduct-Monadic语句组合
很多函数式编程爱好者都把FP称为Monadic Programming,意思是用Monad进行编程.我想FP作为一种比较成熟的编程模式,应该有一套比较规范的操作模式吧.因为Free能把任何F[A]升格 ...