在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阅读 9,397评论 0 7
  • Ruby 安装 要安装cocospods 首先需要安装ruby,可以先安装xcode,再安装macport ,最后...
    山天大畜阅读 5,914评论 0 1
  • 项目组件化、平台化是技术公司的共同目标,越来越多的技术公司推崇使用pod管理第三方库以及私有组件,一方面使项目架构...
    swu_luo阅读 22,524评论 0 39
  • 时光荏苒,犹如白驹过隙,大学的生涯即将结束,原本应该指点江山,激扬文字,自傲的走向社会,开始人生的辉煌,续写社会的...
    smmbulist阅读 4,066评论 0 0
  • 本文参加#感悟三下乡,青春筑梦行#活动,本人承诺,文章内容为原创,且未在其他平台发表过。 曾经我也是个小孩...
    璞二傻阅读 3,613评论 3 5