在swift3.0使用Alamofire

  1. 在項目里新建Podfile文件:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target 'ala03' do
    pod 'Alamofire', '~> 4.4'
end
  1. 執行pod install

  2. 代碼示例

//直接從某個url獲取數據
        
        let testurl = "http://www.xxx.html"
        

        Alamofire.request(testurl).responseJSON { response in
            print("Request: \(String(describing: response.request))")   // original url request
            print("Response: \(String(describing: response.response))") // http url response
            print("Result: \(response.result)")                         // response serialization result
            
            if let json = response.result.value {
                print("JSON: \(json)") // serialized json response
                print("json數據")

            }
            
            if   let data = response.data,  let utf8Text = String(data: data, encoding: .utf8) {
               print("Data: \(utf8Text)") // original server data as UTF8 string
                print("data數據")
             
                }
            }
        }
//從douban api取數據
let URLStringGetBooks = "https://api.douban.com/v2/book/search"
        
Alamofire.request(URLStringGetBooks,method: .post, parameters: ["tag":"Swift","count":1], encoding: JSONEncoding.default, headers: [:]).responseJSON { (resp) -> Void in
            if let error = resp.result.error {
                print(error)
            } else if let value = resp.result.value {
                print(value)
            }

        }
//下載網絡圖片,存放到本地目錄
let url1 = "https://httpbin.org/image/png"
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
Alamofire.download(url1, to: destination)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一. CocoaPods的介紹 什么是CocoaPods?CocoaPods是一個負責管理iOS項目中第三方開源庫...
    輝712閱讀 3,994評論 0 7
  • Ruby 安裝 要安裝cocospods 首先需要安裝ruby,可以先安裝xcode,再安裝macport ,最后...
    山天大畜閱讀 1,897評論 0 1
  • 項目組件化、平臺化是技術公司的共同目標,越來越多的技術公司推崇使用pod管理第三方庫以及私有組件,一方面使項目架構...
    swu_luo閱讀 22,256評論 0 39
  • 時光荏苒,猶如白駒過隙,大學的生涯即將結束,原本應該指點江山,激揚文字,自傲的走向社會,開始人生的輝煌,續寫社會的...
    smmbulist閱讀 755評論 0 0
  • 本文參加#感悟三下鄉,青春筑夢行?;顒樱救顺兄Z,文章內容為原創,且未在其他平臺發表過。 曾經我也是個小孩...
    璞二傻閱讀 581評論 3 5