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了,只是 ...
随机推荐
- C#:Func的同步、异步调用
using System; namespace ActionDemo { class Program { static void Main(string[] args) { Console.Write ...
- C# 与 Java 中的枚举
C#代码: 利用扩展方法,扩展枚举功能 using System; using System.Collections.Generic; using System.Text; using System. ...
- Bootstrap学习笔记系列1-------Bootstrap网格系统
Bootstrap网格系统 学习笔记 [TOC] 简单网格 先上代码再解释 <!DOCTYPE html> <html> <head> <title>B ...
- C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield
IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...
- [转]PHP编码规范
注:这是10年前的一篇PHP编码规范,最早发布于清华水木BBS,现在好像都找不到完整的版本了,但至今看起来仍是非常有参考意义.个人会根据经验做一些调整.文中对于命名一段的描述极大的曾启发了个人的编程体 ...
- Java基础理论知识
package domain; public class Person { private String name; private int age; private char gender; pub ...
- UVALive 4987---Evacuation Plan(区间DP)
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- 线程Thread的基础知识学习
一.线程的基本概念 1.线程是一个程序内部的顺序控制流. 2.Java的线程是通过java.lang.Thread类来实现的. 3.VM启动时会有一个由主方法{public static void m ...
- Monkey测试1——Monkey的使用
Monkey工具使用 一. 什么是Monkey Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实 ...
- cnodejs社区论坛5--话题详情