寫(xiě)在前面
在 這里 可以搜索到最新的 Alamofire~本文中 demo
使用的是 Alamofire4.1.0
版本~點(diǎn)擊 這里 可以下載~
本文項(xiàng)目均基于 xcode 8.1 版本
本文分為兩個(gè)部分:適合對(duì) swift
的初學(xué)者
第一部分~我們一起學(xué)習(xí)怎么一步一步將 Alamofire
導(dǎo)入工程
第二部分~我們一起學(xué)習(xí)怎么使用 Alamofire
文章結(jié)尾作者也做了對(duì) Alamofire
的簡(jiǎn)單封裝~
第一部分,怎么導(dǎo)入工程
- 根據(jù)前面下載一個(gè)版本的
Alamofire
文件~ -
新建一個(gè)工程~
新建工程 - 拷貝
Alamofire
文件到我們新建的工程中~
拷貝文件到工程 -
添加文件到我們自己的工程中~
添加文件1
添加文件2
添加文件3 - 檢查一下我們靜態(tài)庫(kù)有沒(méi)有加到工程沒(méi)有的話我們手動(dòng)添加一下
檢查靜態(tài)庫(kù)1
結(jié)果應(yīng)該是這樣
檢查靜態(tài)庫(kù)2 - 導(dǎo)入頭文件
import Alamofire
檢查是否配置錯(cuò)誤這里可能沒(méi)有提示直接敲完編譯一下就OK了
導(dǎo)入頭文件
第二部分时呀,怎么使用 Alamofire
這是作者的 demo`demo`寫(xiě)的很詳細(xì)直接看demo
就可以了~
這邊我們也分四個(gè)部分來(lái)學(xué)習(xí)先來(lái)上個(gè)圖
第一部分 --> 響應(yīng)鏈
第二部分 --> 請(qǐng)求方式
第三部分 --> 下載
第四部分 --> 上傳
在
demo
中對(duì)應(yīng)的方法為如下圖~對(duì)應(yīng)demo中的方法
這里簡(jiǎn)單貼幾行代碼~
let urlStr = "\(SERVICE_URL)type=\(TOP)&key=\(APPKEY)"
// 1. response()
// 官方解釋:The response handler does NOT evaluate any of the response data. It merely forwards on all information directly from the URL session delegate. It is the Alamofire equivalent of using cURL to execute a Request.
Alamofire.request(urlStr).response { (returnResult) in
if let data = returnResult.data, let utf8Text = String(data: data, encoding: .utf8) {
print("firstMethod --> response() --> utf8Text = \(utf8Text)")
}
}
// 2. responseData()
// 官方解釋:The responseData handler uses the responseDataSerializer (the object that serializes the server data into some other type) to extract the Data returned by the server. If no errors occur and Data is returned, the response Result will be a .success and the value will be of type Data.
Alamofire.request(urlStr).responseData { (returnResult) in
debugPrint("firstMethod --> responseData() --> returnResult = \(returnResult)")
if let data = returnResult.data, let utf8Text = String(data: data, encoding: .utf8) {
print("firstMethod --> responseData() --> utf8Text = \(utf8Text)")
}
}
// 3. responseString()
// 官方解釋:The responseString handler uses the responseStringSerializer to convert the Data returned by the server into a String with the specified encoding. If no errors occur and the server data is successfully serialized into a String, the response Result will be a .success and the value will be of type String.
Alamofire.request(urlStr).responseString { (returnResult) in
debugPrint("firstMethod --> responseString() --> Sucess = \(returnResult.result.isSuccess)")
print("firstMethod --> responseString() --> returnResult = \(returnResult)")
}
// 4. responseJSON()
// 官方解釋:The responseJSON handler uses the responseJSONSerializer to convert the Data returned by the server into an Any type using the specified JSONSerialization.ReadingOptions. If no errors occur and the server data is successfully serialized into a JSON object, the response Result will be a .success and the value will be of type Any.
Alamofire.request(urlStr).responseJSON { (returnResult) in
debugPrint("firstMethod --> responseJSON --> \(returnResult)")
if let json = returnResult.result.value {
print("firstMethod --> responseJSON --> \(json)")
/* 返回請(qǐng)求地址、數(shù)據(jù)晶默、和狀態(tài)結(jié)果等信息
print("firstMethod --> responseJSON() --> \(returnResult.request!)")
print("firstMethod --> responseJSON() --> \(returnResult.data!)")
print("firstMethod --> responseJSON() --> \(returnResult.result)")
*/
}
}
// 1. GET請(qǐng)求
let urlStr = "\(SERVICE_URL)type=\(YULE)&key=\(APPKEY)"
Alamofire.request(urlStr, method: .get).responseJSON { (returnResult) in
print("secondMethod --> GET 請(qǐng)求 --> returnResult = \(returnResult)")
}
// 2. POST請(qǐng)求
Alamofire.request(urlStr, method: .post).responseJSON { (returnResult) in
print("secondMethod --> POST 請(qǐng)求 --> returnResult = \(returnResult)")
}
// 3. 參數(shù)谨娜、編碼
// 官方解釋:Alamofire supports three types of parameter encoding including: URL, JSON and PropertyList. It can also support any custom encoding that conforms to the ParameterEncoding protocol.
let param = [
"type": YULE,
"key" : APPKEY
]
Alamofire.request(SERVICE_URL, method: .post, parameters: param).responseJSON { (returnResult) in
print("secondMethod --> 參數(shù) --> returnResult = \(returnResult)")
}
//Alamofire.request(SERVICE_URL, method: .post, parameters: param, encoding: URLEncoding.default)
//Alamofire.request(SERVICE_URL, method: .post, parameters: param, encoding: URLEncoding(destination: .methodDependent))
// 4.請(qǐng)求頭
let headers: HTTPHeaders = [
"Accept": "application/json"
]
Alamofire.request(urlStr, headers: headers).responseJSON { (returnResult) in
print("secondMethod --> 請(qǐng)求頭 --> returnResult = \(returnResult)")
}
最后附上上面代碼的 demo 地址~以及笨笨自己班門(mén)弄斧基于Alamofire
封裝的 demo
如有問(wèn)題可隨時(shí)給我聯(lián)系歡迎程序媛騷擾~QQ:2638006336