因為iOS的權限限制, 如果使用HTTP協議要配置info.plist, 將Allow Arbitary Loads設為YES。
iOS封裝了URLSession類處理HTTP交互, 支持交互文本、上傳文件、下載文件。
一、 文本交互
一般是用POST請求將包體數據傳給后臺, 后臺返回json包體給手機端, 手機端解析json后做邏輯。
<pre> let urlStr = "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=swift&bk_length=600"
let url = URL(string: urlStr)
var request = URLRequest(url: url!) //請求
request.httpMethod = "POST" //修改http方法
//request.httpBody = Data(bytes: <#T##Array<UInt8>#>) //設置POST包體
let session = URLSession.shared
let date = Date()
print("創建任務, 時間:\(date.timeIntervalSince1970)")
//初始化請求
let dataTask = session.dataTask(with: request,
completionHandler: { (data, resp, err) in
let comDate = Date()
print("http返回, 時間:\(comDate.timeIntervalSince1970)")
if err != nil {
print(err.debugDescription)
} else {
let responseStr = String(data: data!,
encoding: String.Encoding.utf8)
//print(responseStr!) //包體數據
//print("mimeType: (resp?.mimeType) ")
//URLResponse類里沒有http返回值, 需要先強制轉換!
if let response = resp as? HTTPURLResponse {
print("code\ (response.statusCode)")
for (tab, result) in response.allHeaderFields {
print("(tab.description) - (result)")
}
if response.statusCode == 200 {
//JSON解析, 做邏輯
} else {
//通知UI接口執行失敗
}
}
}
} ) as URLSessionTask
let beginDate = Date()
print("開始任務, 時間:\(beginDate.timeIntervalSince1970)")
dataTask.resume() //執行任務
let endDate = Date()
print("結束任務, 時間:\(endDate.timeIntervalSince1970))</pre>
這段代碼說明幾個問題:
1、 request.httpMethod參數可以修改HTTP的方法, 默認是GET。
2、dataTask.resume()是異步執行的,即不阻塞UI。 這里還有閉包的一個概念叫逃逸閉包,對應關鍵字@escapting, 它的意思是將閉包做為回調異步執行(作用類似于Android的Runnable),調用時立刻返回。
<pre> open func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Swift.Void) -> URLSessionDataTask</pre>執行日志:
<pre>創建任務, 時間:1484230880.29283
開始任務, 時間:1484230880.30001
結束任務, 時間:1484230880.30009
http返回, 時間:1484230881.42724 </pre>
3、 http交互成功后要判斷返回值, 作為初學者我翻遍了URLResponse的方法, 就是沒有status code。。。 后來無意中發現了HTTPResponse類, 試著強轉并輸出它的成員變量, 果然好用。
<pre>code 200
Server - Apache
Content-Type - application/json
Transfer-Encoding - Identity
Date - Thu, 12 Jan 2017 14:21:21 GMT
Proxy-Connection - Keep-alive
Tracecode - 12812437700874983946011222</pre>
4、http執行成功后就是要解析包體并做業務邏輯了, responseStr就是我們最終需要的json字符串, 我們需要反序列化并做邏輯。
<pre> if response.statusCode == 200 {
//JSON解析, 做邏輯
} else {
//通知UI接口執行失敗
}</pre>
二、 下載文件, 使用URLSession的API, 代碼很簡單。 重點是存儲位置, iOS會自動生成一個臨時文件。 我們要做的是拷貝這個文件到我們想要的目錄下。
<pre>let url = URL(string: "http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1307/23/c0/23656308_1374564438338_800x600.jpg")
let request = URLRequest(url: url!)
let downloadTask = session.downloadTask(with: request)
downloadTask.resume() //開始下載任務
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("下載結束, 存儲在(location.path)")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
print("total: \(totalBytesWritten), current: \(bytesWritten)") //下載進度
}
</pre>
日志:
<pre> total: 46276, current: 46276
下載結束, 存儲在/Users/brycegao/Library/Developer/CoreSimulator/Devices/8BE9C62E-042E-4B50-8F5D-78F857533650/data/Containers/Data/Application/3A6EB44A-2F88-4E79-9CFC-87713B9FC2E0/tmp/CFNetworkDownload_tnAj6u.tmp
</pre>
三、上傳文件, 因為沒有測試服務器,無法調試。 代碼跟上傳文件類似。
<pre>let uploadTask = session.uploadTask(with: request, from: data) {
(data:Data?, response:URLResponse?, error:Error?) -> Void in
//上傳完畢
if error != nil{
print(error)
}else{
let str = String(data: data!, encoding: String.Encoding.utf8)
print("上傳完畢:(str)") //str是包體
}
}</pre>
小結: iOS對HTTP/HTTPS交互封裝個一套完整方便的API,主要涉及URLSession、URLSesionTask、URLCache及其派生類; 支持文件、上傳/下載文件。