The Question Mark - blog by Mark Volkmann

WeatherKit

Overview

WeatherKit provides a variety of weather data for a given location including condition, temperature, humidity, precipitation, wind, and weather alerts. Usage is free for up to 500,000 calls per month and has a monthly cost beyond that.

WeatherKit requires iOS 16+.

Resources

Setup

  1. Browse developer.apple.com.

  2. Click “Account” and sign in.

  3. Under “Certificates, Identifiers & Profiles”, click “Identifiers”.

  4. Click the ”+” after the heading “Identifiers”.

  5. Select the “App IDs” radio button.

  6. Click the “Continue” button.

  7. Select “App”.

  8. Click the “Continue” button.

  9. Enter an app description.

  10. Paste the app bundle ID.

  11. Under “Capabilities”, check the checkbox for WeatherKit.

  12. Click the “Continue” button.

  13. Click the “App Services” tab.

  14. Check the checkbox for WeatherKit.

  15. Click the “Continue” button.

  16. Click the “Register” button.

  17. In Xcode, click the top entry in the Navigator.

  18. For each target that will use WeatherKit.

    1. Select the target.
    2. Click the “Signing & Capabilities” tab.
    3. Click the ”+” in the upper-left.
    4. Verify that the correct Team is selected which is listed in the upper-right corner of the developer.apple.com web page.
    5. Find WeatherKit and double-click it.

It may be necessary to wait around 30 minutes for the WeatherKit service to be enabled for your app.

Sample Code

See the demo project WeatherKitDemo in GitHub. Here are screenshots from this app:

WeatherKit Current WeatherKit Forecast

WeatherKit Current WeatherKit Heat Map

Temperature Conversion

To create a new Measurement<UnitTemperature> value from an existing one using a different unit, call the converted method. The existing object typically uses the locale-specific unit. For example:

let celsius = temperature.converted(to: .celsius)
let fahrenheit = temperature.converted(to: .fahrenheit)

Limitations

Weather data cannot be retrieved in a preview or in the Simulator. A real device must be used.