测试已测试 | ✓ |
Lang语言 | SwiftSwift |
许可证 | MIT |
Released上次发布 | 2017年1月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Jose Luis Cardosa 维护。
获取您所在城市的当前天气和5天的3小时预报。有用的统计数据,温度,云层,气压,风速……此API是 Open Weather Map 的消费者,API的描述可在此处找到 这里
注意:消费者中只实现了API的免费版本。为了开始使用它,请注册并获得API密钥 注册
要运行示例项目,首先克隆仓库,然后从 Example 目录中运行 pod install
。
基本上,示例使用 CLLocationManager 获取当前的坐标,然后向我们的API发送请求以获取所有显示的信息。所有代码都位于 视图控制器 中。
OpenWeatherMapAPIConsumer 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "OpenWeatherMapAPIConsumer"
import OpenWeatherMapAPIConsumer
// Type by default OpenWeatherMapType.Current
let weatherAPI = OpenWeatherMapAPI(apiKey: "YOUR_API_KEY")
// Type forecast
let weatherAPI = OpenWeatherMapAPI(apiKey: "YOUR_API_KEY", forType: OpenWeatherMapType.Forecast)
public func weather(byLatitude latitude : Double, andLongitude longitude : Double)
public func weather(byCityName cityName : String)
public func weather(byCityName cityName : String, andCountryCode countryCode: String)
// List of city ids may be found here: http://bulk.openweathermap.org/sample/
public func weather(byCityId cityId : Int)
public func setSearchAccuracy(searchAccuracy : SearchAccuracyType)
public func setLimitationOfResult(in limitation : Int)
public func setTemperatureUnit(unit : TemperatureFormat)
public func setMultilingualSupport(language : Language)
public func performWeatherRequest(completionHandler: @escaping (Data?, URLResponse?, Error?) -> Swift.Void)
//Example
weatherAPI.performWeatherRequest(completionHandler:{(data: Data?, urlResponse: URLResponse?, error: Error?) in
if (error != nil) {
//Handling error
} else {
do {
let responseWeatherApi = try CurrentResponseOpenWeatherMap(data: data!)
} catch let error as Error {
//Handling error
}
}
})
响应类是解析器,可以为您提供更好的API响应使用方式
func getCoord() -> CLLocationCoordinate2D
func getTemperature() -> Float
func getPressure() -> Float
func getHumidity() -> Float
func getTempMax() -> Float
func getTempMin() -> Float
func getCityName() -> String
func getIconList() -> IconList
func getDescription() -> String
func getWindSpeed() -> Float
func getDate() -> Date
Jose Luis Cardosa, [email protected]
OpenWeatherMapAPIConsumer 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。