OpenWeatherMapAPIConsumer 0.0.2

OpenWeatherMapAPIConsumer 0.0.2

测试已测试
Lang语言 SwiftSwift
许可证 MIT
Released上次发布2017年1月
SwiftSwift 版本3.0
SPM支持 SPM

Jose Luis Cardosa 维护。



  • 作者
  • Jose Luis Cardosa

OpenWeatherMapAPIConsumer

获取您所在城市的当前天气和5天的3小时预报。有用的统计数据,温度,云层,气压,风速……此API是 Open Weather Map 的消费者,API的描述可在此处找到 这里

注意:消费者中只实现了API的免费版本。为了开始使用它,请注册并获得API密钥 注册

示例

要运行示例项目,首先克隆仓库,然后从 Example 目录中运行 pod install

基本上,示例使用 CLLocationManager 获取当前的坐标,然后向我们的API发送请求以获取所有显示的信息。所有代码都位于 视图控制器 中。

要求

  • iOS 10.2
  • Swift 3
  • Xcode 8

安装

OpenWeatherMapAPIConsumer 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod "OpenWeatherMapAPIConsumer"

用法

在您的文件中导入 Pod

import OpenWeatherMapAPIConsumer

设置 API 密钥

// 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)

API 选项

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响应使用方式

  • CurrentResponseOpenWeatherMap(data: Data)
  • ForecastResponseOpenWeatherMap(data: Data)
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 文件。