Swift OpenWeather Current Weather Data API library

OWMCall is a small Swift library to connect to the OpenWeather Current Weather Data API and retrieve the chosen weather data. Made easy to use with SwiftUI.

Provides for current data through a single function call.

Usage

Weather data from OpenWeather Current Weather Data API is accessed through the use of a OWMProvider, with a single function getWeather, eg:

let weatherProvider = OWMProvider(apiKey: "your key")
@State var weather = OWMResponse()
...

// using a binding
weatherProvider.getWeather(lat: 35.661991, lon: 139.762735, weather: $weather, options: OWMOptions.metric())
...
Text(weather.current?.weatherInfo() ?? "")

// or using the async style, eg with `.task {...}`
if let results = await weatherProvider.getWeather(lat: 35.661991, lon: 139.762735, options: OWMOptions.metric()) {
        weather = results
}

// or using the callback style, eg with `.onAppear {...}`
weatherProvider.getWeather(lat: 35.661991, lon: 139.762735, options: OWMOptions.metric()) { response in
       if let theWeather = response {
          self.weather = theWeather
       }
}

See the following for example uses:

Options

Options available:

Create an options object such as this, to retrieve the current weather data:

let myOptions = OWMOptions(units: .metric, lang: "en")

Installation

Include the files in the ./Sources/OWMCall folder into your project or preferably use Swift Package Manager.

Swift Package Manager (SPM)

Create a Package.swift file for your project and add a dependency to:

dependencies: [
  .package(url: "https://github.com/workingDog/OWMCall.git", from: "1.0.0")
]

Using Xcode

Select your project > Swift Packages > Add Package Dependency...
https://github.com/workingDog/OWMCall.git

Then in your code:

import OWMCall

References

Requirement

Requires a valid OpenWeather key, see:

License

MIT

GitHub

View Github