This is a SwiftUI example application demonstrating all public API methods available in AppleWeatherKit.
- 18 Weather Methods Demonstrated: All forecast, statistics, and summary methods
- Organized UI: Methods grouped by category (Forecast, Statistics, Summary)
- Comprehensive Results: Custom formatting views for each data type
- Error Handling: User-friendly error messages with retry capability
- Educational Comments: Code includes explanations for learning purposes
The Swift files have been created but need to be added to the Xcode project:
- Open
AppleWeatherKitExample.xcodeprojin Xcode - Right-click on the
AppleWeatherKitExamplefolder in the Project Navigator - Select "Add Files to 'AppleWeatherKitExample'..."
- Navigate to the following folders and add them with "Create folder references":
Models/(contains WeatherMethodType.swift and WeatherMethodResult.swift)ViewModels/(contains WeatherViewModel.swift)Views/(contains MethodListView.swift and MethodDetailView.swift)Views/Components/(contains 6 formatting views)
-
Deployment Target: Ensure minimum deployment target is set to iOS 18.0
- Select the project in Project Navigator
- Go to Build Settings
- Search for "Deployment Target"
- Set to iOS 18.0 or later
-
Package Dependencies: Ensure AppleWeatherKit is linked
- Select the project in Project Navigator
- Go to the "Package Dependencies" tab
- AppleWeatherKit should be listed
- If not, add it using File → Add Package Dependencies
Before running the app, you must provide a valid Apple WeatherKit JWT token:
- Open
ExampleApp.swift - Find the
createWeatherConfiguration()method - Replace
"YOUR_JWT_TOKEN_HERE"with your JWT generation code
You need:
- An Apple Developer account
- A WeatherKit service identifier (created in Apple Developer Portal)
- A private key downloaded from Apple Developer Portal
We recommend using vapor/jwt-kit for JWT generation.
Example JWT generation code:
import JWTKit
let signers = JWTSigners()
try! signers.use(.es256(key: .private(pem: privateKeyPEM)))
struct WeatherKitJWT: JWTPayload {
let iss: String // Your Team ID
let sub: String // Your Service Identifier
let exp: ExpirationClaim
let iat: IssuedAtClaim
func verify(using signer: JWTSigner) throws {
try exp.verifyNotExpired()
}
}
let payload = WeatherKitJWT(
iss: "YOUR_TEAM_ID",
sub: "YOUR_SERVICE_ID",
exp: .init(value: Date().addingTimeInterval(3600)),
iat: .init(value: Date())
)
return try! signers.sign(payload, kid: "YOUR_KEY_ID")For more information, see Apple's WeatherKit documentation.
- Select a simulator or device (iOS 18.0+)
- Press Cmd+R to build and run
- Browse the list of weather methods
- Tap any method to execute it and see results
AppleWeatherKitExample/
├── ExampleApp.swift # App entry point with JWT configuration
├── Models/
│ ├── WeatherMethodType.swift # Enum of all available methods
│ └── WeatherMethodResult.swift # Result wrapper enum
├── ViewModels/
│ └── WeatherViewModel.swift # Business logic for API calls
├── Views/
│ ├── MethodListView.swift # Main list of methods
│ ├── MethodDetailView.swift # Detail view with results
│ └── Components/
│ ├── CurrentWeatherView.swift # Current conditions display
│ ├── ForecastView.swift # Hourly/daily/minute forecasts
│ ├── WeatherAlertView.swift # Alert display
│ ├── AvailabilityView.swift # Dataset availability
│ ├── StatisticsView.swift # Weather statistics
│ └── SummaryView.swift # Weather summary
└── ContentView.swift # Original placeholder (can be deleted)
The app uses a hardcoded location for New York City:
- Latitude: 40.7128° N
- Longitude: 74.0060° W
This simplifies the demo by avoiding location permission requirements.
- Full Weather (all datasets)
- Current Weather
- Minute Forecast (next hour)
- Hourly Forecast (24 hours)
- Daily Forecast (10 days)
- Weather Alerts
- Dataset Availability
- Weather Changes
- Historical Comparisons
- Daily Statistics (default range)
- Daily Statistics (custom date range)
- Hourly Statistics (current day)
- Hourly Statistics (custom date range)
- Monthly Statistics (all 12 months)
- Monthly Statistics (custom date range)
- Daily Summary (past 30 days)
- Daily Summary (custom day range)
- Daily Summary (custom date interval)
If you see "No such module 'AppleWeatherKit'":
- Ensure the AppleWeatherKit package is added to the project
- Clean build folder (Cmd+Shift+K)
- Rebuild (Cmd+B)
If all methods fail with authentication errors:
- Verify your JWT token is valid
- Check that your Team ID and Service ID are correct
- Ensure your private key matches the Key ID in the JWT header
- Verify the JWT hasn't expired
If SwiftLint is configured, you may see warnings. The code follows standard Swift conventions and can be auto-fixed with swiftlint --fix.
This example app is provided as part of the AppleWeatherKit library for demonstration purposes.