A lightweight, async‑await‑based Swift wrapper for the OpenWeather API.
It provides type‑safe models for current weather, 5‑day forecasts, and air‑pollution data, along with convenient enums for units and language selection.
- ✅ Modern Swift
- ✅ Automatic conversion of timestamps to
Dateobjects - ✅ Built‑in support for all OpenWeather language codes and units (metric, imperial, standard)
- Swift 5.8 or later
- iOS 13+, macOS 10.15+, watchOS 6+, tvOS 13+ (any platform that supports Swift Concurrency)
- An OpenWeather API key
Add the package to your project’s Package.swift or via Xcode:
.package(url: "https://github.com/your‑username/swift-open-weather.git", from: "1.0.0")
.target(
name: ....,
dependencies: [.product(name: "OpenWeather", package: "OpenWeatherSwiftSDK")]
)import OpenWeather
// 1️⃣ Create the client with your API key
let weatherClient = OpenWeather(apiKey: "YOUR_API_KEY")
// 2️⃣ Fetch the current weather for a location
do {
let current = try await weatherClient.current(
lat: 37.7749,
lon: -122.4194,
lang: .English, // optional – defaults to English
units: .currentLocale // optional – defaults to locale‑aware units
)
print("Temperature: \(current.main.temp)°")
print("Condition: \(current.weather.first?.label ?? "N/A")")
print("Icon URL: \(current.weather.first?.iconUrl?.absoluteString ?? "none")")
} catch {
print("❌ Failed to fetch weather:", error)
}do {
let forecast = try await weatherClient.forecast(
lat: 37.7749,
lon: -122.4194
)
// Each `Item` represents a 3‑hour block
for item in forecast.list.prefix(5) {
print("\(item.date): \(item.main.temp)°, \(item.weather.first?.label ?? "")")
}
} catch {
print("❌ Forecast error:", error)
} do {
let air = try await weatherClient.airPollutions(
lat: 37.7749,
lon: -122.4194
)
if let first = air.list.first {
print("AQI: \(first.main.aqi) – \(first.main.airQualityIndex?.description ?? "unknown")")
print("Components: CO \(first.components.co) µg/m³, PM2.5 \(first.components.pm2_5) µg/m³")
}
} catch {
print("❌ Air pollution error:", error)
}OpenWeather data is provided under the OpenWeather terms of service. When displaying weather information, include the required attribution:
let attribution = OpenWeather.attribution
print("\(attribution.title) – \(attribution.url)")