Swift2.0封装http请求
由于现在Swift2.0是beta版本,一些github上网络请求的框架不能使用,自己封装了一下!!
有问题可以发送邮件[email protected], QQ群:383126909 IT江湖
感谢@johnlui 提供的Swift-On-iOS,Alamofire框架的函数
在Info.plist文件中添加如下代码
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitrayLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>www.weather.com.cn</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
- iOS 9.0 / Mac OS X 10.11
- Xcode7 bete4
- Swift2.0 beta
pod 'HttpSwift2.0', '~> 1.3'
import HttpSwift
HttpSwift.request("get", url: url) { (data, response, error) -> Void in
//使用guard判断
guard error != nil else{
print(data)
return
}
}
返回结果
{
"weatherinfo":
{"city":"太仓","cityid":"101190408","temp":"13","WD":"西北风","WS":"3级","SD":"93%","WSE":"3","time":"10:20","isR adar":"0","Radar":"","njd":"暂无实况","qy":"1005"
}
}
/*测试POST请求*/
params:请求参数
HttpSwift.request("POST", url: url, params: ["post": "value"]) { (data, response, error) -> Void in
//使用guard判断
guard error != nil else{
print(data)
return
}
}
- POST不带参数
HttpSwift.post(url, callback: { (data, response, error) -> Void in
//使用guard判断
guard error != nil else{
print(data)
print("POST不带参数 请求成功")
return
}
})
- POST带参数
HttpSwift.post(url, params: ["post": "POST Network"], callback: { (data, response, error) -> Void in
let string = data
//使用guard判断
guard error != nil else{
print(data)
print("POST 2 请求成功 \(string)")
return
}
})
- GET不带参数
HttpSwift.get(url, callback: { (data, response, error) -> Void in
let string = data
//使用guard判断
guard error != nil else{
print(data)
print("GET不带参数 请求成功 \(string)")
return
}
})
- GET带参数
HttpSwift.get(url, params: ["get": "POST Network"], callback: { (data, response, error) -> Void in
let string = data
//使用guard判断
guard error != nil else{
print(data)
print("GET带参数 请求成功 \(string)")
return
}
})
- PUT带参数
HttpSwift.put(url, params: ["put": "POST Network"], callback: { (data, response, error) -> Void in
let string = data
//使用guard判断
guard error != nil else{
print(data)
print("put带参数 请求成功 \(string)")
return
}
})
- DELETE带参数
HttpSwift.delete(url, params: ["id":122]) { (data, response, error) -> Void in
guard error != nil else{
print(data)
print("DELETE带参数 请求成功\(data)")
return
}
}
未完待续 PUT DELETE等请求方法