Swift Alamofire

it2024-07-15  69

转载:https://www.jianshu.com/p/07b1ec36a689最近AFNetworking的作者Matt Thompson 提出了一个新的类似AFNetworking的网络基础库,并且专门使用最新的Swift语言写的,名为 Alamofire.

 

一、正常导入,CocoaPods

1-1、注意下CocoaPods版本

gem install cocoapods

CocoaPods 0.39.0+ is required to build Alamofire 3.0.0+.

1-2、vim Podfile platform :ios, '8.0' use_frameworks! pod 'Alamofire' //然后 pod install 就OK了

 

1-3、导入Alamfire 就可以正常使用了 import Alamofire 注意目前可能会出现这个警告;Cannot load underlying module for 'Alamofire',可以先忽略它,直接 build就没了

 

 

二、基本使用

GET请求

普通的get请求

下面是一个天气预报的请求,时间久了,key 会失效 let parameters:Dictionary = ["key":"93c921ea8b0348af8e8e7a6a273c41bd"] Alamofire.request(.GET, "http://apis.haoservice.com/weather/city", parameters: parameters) .responseJSON { response in print("result==\(response.result)") // 返回结果,是否成功 if let jsonValue = response.result.value { /* error_code = 0 reason = "" result = 数组套字典的城市列表 */ print("code: \(jsonValue["error_code"])") } } /* result==SUCCESS code: Optional(0) */

 

带head的get请求

let headers = ["apikey":"a566eb03378211f7dc9ff15ca78c2d93"] Alamofire.request(.GET, "http://apis.baidu.com/heweather/pro/weather?city=beijing", headers: headers) .responseJSON { response in print("result==\(response.result)") if let jsonValue = response.result.value { print("weNeedReuslt == \(jsonValue)") } }

 

POST 请求

先看看Alamofire 定义了许多其他的HTTP 方法(HTTP Medthods)可以使用。

public enum Method: String { case OPTIONS, GET, HEAD, POST, PUT, PATCH, DELETE, TRACE, CONNECT }

 

使用GET类型请求的时候,参数会自动拼接在url后面,使用POST类型请求的时候,参数是放在在HTTP body里传递,url上看不到的

let parameters:Dictionary = ["key":"93c921ea8b0348af8e8e7a6a273c41bd"] Alamofire.request(.POST, "http://apis.haoservice.com/weather/city", parameters: parameters) .responseJSON { response in print("result==\(response.result)") // 返回结果,是否成功 if let jsonValue = response.result.value { /* error_code = 0 reason = "" result = 数组套字典的城市列表 */ print("code: \(jsonValue)") } }

 

至于加header的post 请求,实际上也是GET 一样的

注意点1: 参数编码方式

除了默认的方式外,Alamofire还支持URL、URLEncodedInURL、JSON、Property List以及自定义格式方式编码参数。

public enum ParameterEncoding { case URL case URLEncodedInURL case JSON case PropertyList(NSPropertyListFormat, NSPropertyListWriteOptions) case Custom((URLRequestConvertible, [String: AnyObject]?) -> (NSMutableURLRequest, NSError?)) }

 

//想要把一个字典类型的数据,使用json格式发起POST请求

let parameters = [ "one": [1,2,3], "two": ["apple": "pig"] ] Alamofire.request(.POST, "http://www.example.com/service", parameters: parameters, encoding: .JSON)

 

注意点2:validate()

将其与请求和响应链接,以确认响应的状态码在默认可接受的范围(200到299)内。如果认证失败,响应处理方法将出现一个相关错误,我们可以根据不同在完成处理方法中处理这个错误。比如下面的样例,成功时会打印成功信息,失败时输出具体错误信息。

Alamofire.request(.GET, "http://apis.haoservice.com/weather/city", parameters: ["apikey":"a566eb03378211f7dc9ff15ca78c2d93"]) .validate() .responseJSON { response in switch response.result { case .Success: print("数据获取成功!") case .Failure(let error): print(error) } }

 

注意点3:响应处理方法

观察上面几个请求,我都是使用样例的responseJSON(处理json类型的返回结果)外,Alamofire还提供了许多其他类型的响应处理方法:

response() responseData() responseString(encoding: NSStringEncoding) responseJSON(options: NSJSONReadingOptions) responsePropertyList(options: NSPropertyListReadOptions)

我们可以根据我的实际情况,选择自己需要的。

例如 responseData() Alamofire.request(.GET, "http://apis.haoservice.com/weather/city", parameters: ["apikey":"a566eb03378211f7dc9ff15ca78c2d93"]) .responseData { response in print(response.request) print(response.response) print(response.result) }

暂时基本使用,总结到此,持续更新中····⛽️

备注参考

https://github.com/Alamofire/Alamofirehttp://www.hangge.com/blog/cache/detail_970.htmlhttp://www.cnblogs.com/iCocos/p/4550570.html

转载于:https://www.cnblogs.com/lishanshan/p/8464874.html

相关资源:swift-基于Alamofire封装的网络请求工具
最新回复(0)