_WORLD__REST__API__!
A lightweight Swift library for making web requests and consuming RESTful APIs!
Add GTRest to Your Project
Using CocoaPods
在您的Podfile中添加:
pod `GTRest`
别忘了在任何你想在项目中使用GTRest的地方导入它
import GTRest
手动
下载或克隆仓库,并将GTRest/Source目录中的文件添加到您的项目中。
Usage
使用GTRest进行网络请求非常简单直接!
查看由 jazzy 生成的 jazzy 文档,了解如何使用 GTRest。
快速示例
let url = URL(...) // A URL object.
let rest = GTRest()
// Set any request HTTP headers:
rest.requestHttpHeaders.add(value: "application/json", forKey: "Accept")
// Set any URL query parameters:
rest.urlQueryParameters.add(value: "2", forKey: "page")
// Make a request.
rest.makeRequest(toURL: url, httpMethod: .get) { [unowned self] (results) in // or [weak self] (results) in
// Access data returned by the server:
if let data = results.data {
// Perform app-specific actions
}
// Access the response:
if let response = results.response {
// Do something with the response object if necessary.
// Checking the HTTP status code :
if (200...299).contains(response.httpStatusCode) {
// Successful request.
} else { ... }
}
// Access the error:
if let error = results.error {
// Do something with the error.
}
// Always update your UI on main thread:
DispatchQueue.main.async {
// Update UI.
}
}
需求
iOS 11.0 以上。
另请参阅
你可能还对 GTNetMon 感兴趣,它是一个 Swift 库,用于获取网络状态和连接信息,并监控网络变化。