超簡單 Alamofire4.1 的詳細使用步驟

寫在前面


這里 可以搜索到最新的 Alamofire~本文中 demo 使用的是 Alamofire4.1.0版本~點擊 這里 可以下載~
本文項目均基于 xcode 8.1 版本
本文分為兩個部分:適合對 swift 的初學者
第一部分~我們一起學習怎么一步一步將 Alamofire 導入工程
第二部分~我們一起學習怎么使用 Alamofire 文章結尾作者也做了對 Alamofire 的簡單封裝~

第一部分,怎么導入工程


  1. 根據前面下載一個版本的 Alamofire 文件~
  2. 新建一個工程~


    新建工程
  3. 拷貝 Alamofire 文件到我們新建的工程中~
    拷貝文件到工程
  4. 添加文件到我們自己的工程中~


    添加文件1

    添加文件2

    添加文件3
  5. 檢查一下我們靜態庫有沒有加到工程沒有的話我們手動添加一下
    檢查靜態庫1

    結果應該是這樣
    檢查靜態庫2
  6. 導入頭文件import Alamofire檢查是否配置錯誤這里可能沒有提示直接敲完編譯一下就OK了
    導入頭文件

第二部分,怎么使用 Alamofire

這是作者的 demo`demo`寫的很詳細直接看demo就可以了~
這邊我們也分四個部分來學習先來上個圖

第一部分 --> 響應鏈

第二部分 --> 請求方式

第三部分 --> 下載

第四部分 --> 上傳

demo中對應的方法為如下圖~
對應demo中的方法

這里簡單貼幾行代碼~
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)")
            /*  返回請求地址、數據、和狀態結果等信息
             print("firstMethod --> responseJSON() --> \(returnResult.request!)")
             print("firstMethod --> responseJSON() --> \(returnResult.data!)")
             print("firstMethod --> responseJSON() --> \(returnResult.result)")
             */
        }
    }

    // 1. GET請求
    let urlStr = "\(SERVICE_URL)type=\(YULE)&key=\(APPKEY)"
    Alamofire.request(urlStr, method: .get).responseJSON { (returnResult) in
        print("secondMethod --> GET 請求 --> returnResult = \(returnResult)")
    }

    // 2. POST請求
    Alamofire.request(urlStr, method: .post).responseJSON { (returnResult) in
        print("secondMethod --> POST 請求 --> returnResult = \(returnResult)")
    }

    // 3. 參數、編碼
    // 官方解釋: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 --> 參數 --> 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.請求頭
    let headers: HTTPHeaders = [
        "Accept": "application/json"
    ]
    Alamofire.request(urlStr, headers: headers).responseJSON { (returnResult) in
        print("secondMethod --> 請求頭 --> returnResult = \(returnResult)")
    }

最后附上上面代碼的 demo 地址~以及笨笨自己班門弄斧基于Alamofire封裝的 demo
如有問題可隨時給我聯系歡迎程序媛騷擾~QQ:2638006336

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容