Request OpenWeatherMap data with ease in Swift

HPOpenWeather

HPOpenWeather is a cross-platform Swift framework to communicate with the OpenWeatherMap JSON API.

Installation

HPOpenWeather supports iOS 9.0+, watchOS 2.0+, tvOS 9.0+ and macOS 10.10+.

SPM

Add .package(url: "https://github.com/henrik-dmg/HPOpenWeather", from: "3.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 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

// Assign API key
HPOpenWeather.shared.apiKey = "--- YOUR API KEY ---"

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 = TimeMachineRequest(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 the requestWeather on HPOpenWeather:

HPOpenWeather.shared.requestWeather(request) { result in
	switch result {
    case .success(let response):
    	// do something with weather data here
    case .failure(let error):
        // handle error
    }
}

The following response languages are available

  • English (default)
  • 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 (default)
  • Kelvin
  • Fahrenheit

TODO List

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

GitHub