WeatherKit
Fetch current conditions, hourly and daily forecasts, weather alerts, and historical statistics using WeatherService. Displays mandatory Apple Weather attribution.
Contents
- Setup & Permissions
- Fetching Weather Data
- Selective Queries
- Context & Historical Queries (iOS 18+)
- Attribution Requirements
- Caching & Rate Limits
- Common Mistakes
- Review Checklist
- References
Setup & Permissions
- Enable the WeatherKit capability in Xcode.
- Enable WeatherKit for your App ID in the Apple Developer portal.
- Add
NSLocationWhenInUseUsageDescriptionto Info.plist if requesting coordinates via CoreLocation. - Use
WeatherService.sharedacross the app.
Fetching Weather Data
Fetch complete weather datasets for a target CLLocation:
import WeatherKit
import CoreLocation
let weather = try await WeatherService.shared.weather(for: location)
// Current conditions
let current = weather.currentWeather
print("Temperature: \(current.temperature.formatted())")
print("Condition: \(current.condition.description), SF Symbol: \(current.symbolName)")
// Hourly and Daily forecasts
for hour in weather.hourlyForecast.prefix(12) {
print("\(hour.date.formatted(date: .omitted, time: .shortened)): \(hour.temperature.formatted())")
}
Format Measurement<UnitTemperature> values with .formatted() to honor user locale conventions.
Selective Queries
Reduce bandwidth and processing by requesting only necessary datasets:
let (current, hourly) = try await WeatherService.shared.weather(
for: location,
including: .current, .hourly
)
Available query types include .current, .hourly, .daily, .minute, and .alerts.
Context & Historical Queries (iOS 18+)
Access climate context, historical comparisons, and weather summaries:
// Query weather changes (e.g. temperature shift since yesterday)
let changes = try await WeatherService.shared.weather(for: location, including: .changes)
// Historical comparison
let historical = try await WeatherService.shared.weather(for: location, including: .historicalComparisons)
Attribution Requirements
Apple requires visible attribution whenever WeatherKit data is displayed:
let attribution = try await WeatherService.shared.attribution
// Display attribution.combinedMark (light/dark Apple Weather logo)
// Provide tappable link to attribution.legalPageURL
In SwiftUI, display attribution using Link and the official logo asset.
Caching & Rate Limits
- Respect
metadata.expirationDatereturned with forecasts; avoid refetching unexpired data. - Batch requests by coordinates and debounce location updates to stay within API tier allowances.
Common Mistakes
- Omitting mandatory Apple Weather attribution: App Store review rejects apps displaying WeatherKit data without legal attribution and logo.
- Ignoring forecast expiration dates: Calling
weather(for:)on every view appearance causes rate-limiting errors. Respectmetadata.expirationDate. - Hardcoding temperature unit conversion: Manually calculating Fahrenheit/Celsius ignores system locale. Use
temperature.formatted(). - Requesting all datasets when only current weather is needed: Increases network latency. Use selective
including:queries. - Missing Apple Developer WeatherKit entitlement: WeatherKit calls fail if the capability is not enabled in the developer portal.
Review Checklist
- WeatherKit capability enabled in Xcode and Developer Portal
- Required Apple Weather attribution logo and legal link displayed
- Temperatures formatted via
Measurement.formatted() - Selective queries (
including:) used to minimize network payload - Forecasts cached and refreshed according to
metadata.expirationDate
References
- Extended patterns (SwiftUI dashboard, charts integration, historical statistics): references/weatherkit-patterns.md
- WeatherKit framework
- WeatherService
- WeatherAttribution
- WeatherQuery
- WeatherQuery.daily(startDate:endDate:)
- WeatherQuery.hourly(startDate:endDate:)
- CurrentWeather
- CurrentWeather.temperature
- Measurement.formatted()
- Forecast
- HourWeather
- DayWeather
- WeatherAlert
- WeatherAvailability
- WeatherMetadata.expirationDate
- WeatherQuery.changes
- WeatherQuery.historicalComparisons
- WeatherKit updates
- Bring context to today's weather
- WeatherService.weather(for:)