HPOpenWeather

master

Request OpenWeatherMap data with ease in Swift
thierryH91200/HPOpenWeather

Storage

codebeat badge

HPOpenWeather is a cross-platform Swift framework to communicate with the OpenWeatherMap JSON API. See their documentation for further details.

Installation

HPOpenWeather supports iOS 9.0+, watchOS 2.0+, tvOS 9.0+ and macOS 10.10+. To install simply add pod 'HPOpenWeather' to your Podfile, or add github "henrik-dmg/HPOpenWeather" ~> 2.0.2

Usage

To get started, you need an API key from OpenWeatherMap. 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

var api = HPOpenWeather(apiKey: "--- YOUR API KEY ---")

The following response languages are available

  • English
  • Russian
  • Italian
  • Spanish
  • Ukrainian
  • German
  • Portuguese
  • Romanian
  • Polish
  • Finnish
  • Dutch
  • French
  • Bulgarian
  • Swedish
  • Chinese Traditional
  • Chinese Simplified
  • Turkish
  • Croatian
  • Catalan

The following temperature units are available

  • Celsius
  • Kelvin
  • Fahrenheit

Creating a request

To make an request, create a new WeatherRequestobject, such as:

// This will request weather data based on coordinates
var locationRequest = LocationRequest(userCoordinates)

/*
This will request weather data based on a city name.
countryCode is an optional ISO 3166 code to narrow the search down
*/
var cityRequest = CityNameRequest("Berlin", countryCode: "DE")

/*
This will request weather data based on a ZIP code
Again, countryCode is an optional ISO 3166 code
*/
var zipCodeRequest = ZipCodeRequest("10405", countryCode: "DE")

/*
This will request weather data based on a city ID.
For a list of city IDs, follow the link below
*/
var cityIdRequest = CityIdRequest("2950159")

Full list of city IDs can be downloaded here

Executing the request

Currently, only current weather data, and forecast data are available (historical data support will be added later). To request the current weather, call

api.requestCurrentWeather(with: request) { (current, error) in
	guard let current = current, error == nil else {
		// Handle error here
		return
	}
	
	// Do something with data here
}

Note: The type returned by this call is CurrentWeatherwhich is slightly different from HourlyForecastand DailyForecast.

To request an hourly forecast, call requestHourlyForecast(...)and requestDailyForecastrespectively.

api.requestHourlyForecast(with: request, for: .threeHourly) { (forecast, error) in
	guard let forecast = forecast, error == nil else {
		// Handle error here
		return
	}
            
	// Do something with forecast here
}

TODO List

  • Current weather data
  • Daily and hourly forecast
  • More Unit Tests
  • Historical Data
  • UV Index Data
  • watchOS and tvOS demo apps

This documentation is far from complete, however the code itself is pretty well documented so feel free to just play around and just contact me if you have any suggestions :)

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Oct 21 2024 17:13:49 GMT-0900 (Hawaii-Aleutian Daylight Time)