HPOpenWeather
HPOpenWeather is a cross-platform Swift framework to communicate with the OpenWeather One-Call API. See their documentation for further details.
Installation
HPOpenWeather supports iOS 13.0+, watchOS 7.0+, tvOS 13.0+ and macOS 10.15+.
SPM
Add .package(url: "https://github.com/henrik-dmg/HPOpenWeather", from: "5.0.0")
to your Package.swift
file
CocoaPods
Add pod 'HPOpenWeather'
to your Podfile
and run pod install
Usage
To get started, you need an API key from OpenWeather. Put this API key in the initialiser, additionally you can also specify a custom temperature format and/or language used in the responses (see list for available languages and units below).
import HPOpenWeather
// Assign API key
OpenWeather.shared.apiKey = "--- YOUR API KEY ---"
OpenWeather.shared.language = .german
OpenWeather.shared.units = .metric
// Or use options
let settings = OpenWeather.Settings(apiKey: "yourAPIKey", language: .german, units: .metric)
OpenWeather.shared.apply(settings)
You can also customise the response data units and language by accessing the language
and units
propertis.
Making a request
To make a request, initialize a new request object like this
let request = WeatherRequest(coordinate: .init(latitude: 40, longitude: 30))
Or to request weather data from the past:
let timemachineRequest = WeatherRequest(coordinate: .init(latitude: 40, longitude: 30), date: someDate)
Note: the date has to be at least 6 hours in the past
To post a request, call sendWeatherRequest
on OpenWeather
:
// Classic completion handler approach
OpenWeather.shared.schedule(request) { result in
switch result {
case .success(let response):
// do something with weather data here
case .failure(let error):
// handle error
}
}
// Or using the new concurrency features
let response = try await OpenWeather.shared.weatherResponse(request)
Available languages (default in bold)
- English
- Russian
- Italian
- Spanish
- Ukrainian
- German
- Portuguese
- Romanian
- Polish
- Finnish
- Dutch
- French
- Bulgarian
- Swedish
- Chinese Traditional
- Chinese Simplified
- Turkish
- Croatian
- Catalan
Available units (default in bold)
- Metric (wind speed in m/s, temperature in Celsius)
- Imperial (wind speed in mph, temperature in Fahrenheit)
- Standard (wind speed in m/s, temperature in Kelvin)