Getting started with iOS 16 WeatherKit

Will be there sun or rain on your iPhone?

Giulio Caggegi
4 min readDec 1, 2022

Recently Apple introduced a new framework, WeatherKit. The framework allows developers to query the Apple servers to fetch weather information. I’ll try to explain how work and what offers these new APIs. Let’s go!

Usage Fee

The new WeatherKit APIs aren’t totally free. Apple provides 500.000 API calls a month if you enrolled in the Developer Membership Program. If you need to exceed this limit you can subscribe to one of the plans provided for this service. All the subscription information and the related prices are available at this site:

Capabilities

In order to make the API calls you need to add the entitlement on the Apple Developer Portal and the capability in your project. The steps to make this are:

  • Sign in to developer.apple.com/account.
  • Search Certificates, Identifiers & Profiles on the left menu.
  • Search Identifiers on the left menu.
  • Click on the “+” button to create a new identifier.
  • Create a new AppID for the app.
  • At this point, you should see the “Register an App ID” screen. Now you can select WeatherKit in the Capabilities menu.
  • Switch to App Services menu and select WeatherKit.
  • NOTE: it’s important now, according to Apple documentation, wait for at least 30 minutes for the service to register your app.
  • Open your Xcode project, and add the capability for WeatherKit.

Finally, you are able to call the WeatherKit API.

Attribution

Before all, notes that if you want to use the WeatherKit API in an AppStore app, you must show the attribution. Apple provides an object for this, which is WeatherAttribution. This object contains two properties, an image, and a link. The image is provided through an URL and is available in various variants; so we will have the combinedMarkDarkURL and the combinedMarkLightURL. The link contains a link to the legal attribution page that contains copyright information about the weather data sources.

The attribution object can be fetched in this way:

func getAttribution() async throws -> WeatherAttribution {
let attribution = try await WeatherService.shared.attribution
}

As you can see, the attribution method throws the WeatherAttribution object in an async way.

Fetch the weather information

The weather data provider it’s a simple class that encapsulates all the methods to fetch the information, that is WeatherService. You can instantiate a new WeatherService class, or use the single shared singleton provided by Apple:

/// The Apple weather service.
private let weatherService: WeatherService = {
WeatherService()
}()

/// The Apple weather sahred service.
private let sharedWeatherService = WeatherService.shared

This class has a single method that returns the weather forecast for the requested location:

weather<T>(for location: CLLocation, including dataSet: WeatherQuery<T>) async throws -> T

This is a dynamic API that contains more than a query, and returns the forecast as a tuple. Let’s see some examples:

let location = try await locationWorker.getCurrentLocation()
let weather = try await service.weather(for: location, including: .current)
let location = try await locationWorker.getCurrentLocation()
let weather = try await service.weather(for: location, including: .current, .alerts, .daily)

In the first example, we requested(in an async way) the current user location and the current forecast weather passing the .current query.

In the second example, we requested the current forecast weather, the alerts, and the daily forecast. In this case, the result will be returned in a tuple, so we will access the current forecast with weather.0 , the alerts with weather.1, and the daily forecasts with weather.2 . Apple provides a big set of weather queries:

The weather queries provided by Apple

Notes that iOS provides a method to format and localize the data in your local region. To do this, you can call the formatted API:

weather.temperature.formatted(.measurement(width: .wide, usage: .weather))

CurrentWeather and DailyForecast

The CurrentWeather object includes a lot of information to show, such as:

  • temperature: the current temperature of the air.
  • condition: an enumeration that represents the condition at the time.
  • apparentTemperature: the feel-like temperature.
  • symbolName: a useful string indicating the current weather system symbol to use to represent the weather condition.
  • cloudCover: the percentage(between 0 and 1) of the covered sky.
  • dewPoint: the temperature at which relative humidity is 100%.
  • humidity: the humidity of the air.
  • isDaylight: a boolean indicating if there is daylight.
  • wind: an object with wind, direction, and speed.
  • visibility: the distance at which terrain is visible.

and moreover.

Also the daily weather forecast contains a lot of useful information. Some examples are:

  • moon: the information about the moon.
  • sun: the information about the sunrise and the sunset.
  • precipitationChance: the chance of rain.
  • highTemperature: the maximum temperature of the day.
  • lowTemperature: the lowest temperature of the day.

and moreover.

REST API

Over the iOS APIs, Apple released in beta also the WeatherKit REST API. So we can use the same weather information in a web app, on Android, or another platform. You can find all the information about the REST API here:

Next Steps

Now you know the basics of the new WeatherKit framework; you can explore it, test it and use it in a lot of ways. Enjoy yourself with the new WeatherKit Framework.

--

--