WWDC22: WeatherKit 初探

发布
更新
字数 605
阅读 4 分钟
阅读量 1770

WeatherKit 提供了全面的全球天气数据,可以很方便地在应用或其他服务中使用。WeatherKIt 提供了原生 Swift API 和 REST API 两种访问方式。可以获取当前和10天内的分时天气预报情况,包含温度、预计降水量、风速、UV 指数等等,以及更多的详细信息。

WeatherKit / Current weather / Minute forecast / Hourly forecast / 
Daily forecast / Weather alerts / Historical weather / UV index / 
Humidity / Pressure / Moonset Precipitation chance / Cloud cover / 
Visibility / Condition temperature / Wind gust / Sunrise / Pressure 
trend / Wind speed / Moonrise / Weather severity / Precipitation 
intensity / Precipitation amount / High temperature / Wind direction / 
Low temperature / Dew point / Apparent temperature / Moon shape / 
Sunset / Snowfall amount

WeatherKit 文档:https://developer.apple.com/documentation/WeatherKit

Weather data

  • Currrent weather
    • Apparent temperature
    • Cloud cover
    • Condition
    • Dew point
    • Humidity
    • Pressure
    • Pressure trend
    • Temperature
    • UV index
    • Visibility
    • Wind direction
    • Wind gust
    • Wind speed
  • Minute forecast,包含一小时内的降雨情况
    • Precipitation chance
    • Precipitation intensity
  • Hourly forecast,包含未来 240 小时内的数据,数据结构同 current weather
  • Daily forecast,包含未来 10 天内的数据
    • High temperature
    • Low temperature
    • Moon phase
    • Moonset
    • Precipitaion amount
    • Snowfall amount
    • Sunrise
    • Sunset
    • Wind direction
  • Weather alerts
    • Region
    • Severity
    • Source
    • Summary
  • Historical weather,daily or hourly
    • Apparent temperature
    • Cloud cover
    • Condition
    • Dew point
    • High temperature
    • Humidity
    • Low temperature
    • Moon phase
    • Moonrise
    • Moonset
    • Precipitation amount
    • Precipitation chance
    • Precipitation intensity
    • Pressure
    • Pressure trend
    • Snowfall amount
    • Sunrise
    • Sunset
    • Temperature
    • UV index
    • Visibility
    • Wind direction
    • Wind gust
    • Wind speed

WeatherKit

通过 swift WeatherKit 框架使用 Apple Weather Service 非常方便,只需要为 weather service 提供一个地理位置即可。得力于 swift 异步编程的能力,整个过程非常的直观。

// Request the weather

// 1. 导入
import WeatherKit
import CoreLocation

// 2. 创建 weather service 实例
let weatherService = WeatherService()

// 3. 获取天气的地理信息
let syracuse = CLLocation(latitude: 43, longitude: -76)

// 4. 使用异步 API 获取天气数据
let weather = try! await weatherService.weather(for: syracuse)

// 5. 访问信息
let temperature = weather.currentWeather.temperature

let uvIndex = weather.currentWeather.uvIndex

要使用 WeatherKit,需要为 App Identifier 开启 WeatherKit capability,然后就可以使用 weather(for:, including:) 获取天气数据了。

WeatherKit REST API

Swift 代码示例

/* Request a token */
// 需要在 Developer Portal 创建 private key
// 然后创建 JWT token
const tokenResponse = await fetch('https://example.com/token');
const token = await tokenResponse.text();

/* Get my weather object */
const url = "https://weatherkit.apple.com/1/weather/en-US/41.029/-74.642?dataSets=weatherAlerts&country=US"

const weatherResponse = await fetch(url, {
    headers: {
        "Authorization": token
    }
});
const weather = await weatherResponse.json();

/* Check for active weather alerts */
const alerts = weather.weatherAlerts;
const detailsUrl = weather.weatherAlerts.detailsUrl;

注意版权

使用 Apple Weather Service WeatherKit 需要显示 Weather 徽标和版权链接。