Secret-Of-SwiftUI / SSDC22

๐Ÿคก ์‘~ ์งˆ๋ ค? deprecated ํ•˜๋ฉด ๊ทธ๋งŒ์ด์•ผ~
9 stars 0 forks source link

Meet WeatherKit #11

Open Taehyeon-Kim opened 2 years ago

Taehyeon-Kim commented 2 years ago

WeatherKit offers valuable weather data for your apps and services to help people stay up to date on the latest conditions. Learn how to use Swift and REST APIs to access information about the current weather, 10-day hourly forecasts for temperature, expected precipitation, wind reports, the UV Index, and more. We'll also share how WeatherKit can provide timely, hyperlocal weather information without compromising someone's personal data or their privacy.

Taehyeon-Kim commented 2 years ago

WeatherKit์„ ๋งŒ๋“  ์ด์œ 

๋‚ ์”จ ์ •๋ณด์˜ ์ค‘์š”์„ฑ

์šฐ๋ฆฌ๋Š” ๋‚ ์”จ ๋ฐ์ดํ„ฐ์— ์˜์กดํ•˜๋ฉฐ ์‚ฝ๋‹ˆ๋‹ค. ์ƒํ™ฉ์„ ํ•œ ๋ฒˆ ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ƒํ™ฉ์— ์ด๋ฅด๊ธฐ๊นŒ์ง€ ๋‚ ์”จ๋Š” ๋ชจ๋“  ์‚ฌ๋žŒ์—๊ฒŒ ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š”๋ฐ์š”.์ด์— ๋”ฐ๋ผ์„œ ์ •ํ™•ํ•œ ๊ธฐ์ƒ ๋ฐ์ดํ„ฐ๋Š” ๋ณ€ํ™”ํ•˜๋Š” ๊ธฐํ›„์˜ ์˜ํ–ฅ์„ ๋งŽ์ด ๋ฐ›๋Š” ์˜ค๋Š˜๋‚ ์˜ ์„ธ๊ณ„์— ๋”์šฑ ์ค‘์š”ํ•ด์กŒ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ WeatherKit์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

WeatherKit

์œ„์˜ ๊ธฐ์ˆ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ „ ์„ธ๊ณ„์˜ ์ดˆ์ง€์—ญ ๊ธฐ์ƒ ์˜ˆ๋ณด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Apple Weather Service๋ฅผ ํ†ตํ•ด์„œ ๋งŽ์€ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ •ํ™•ํ•œ ๋‚ ์”จ ๋ฐ์ดํ„ฐ๋Š” ์œ„์น˜ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๊ณต๊ฐœ๋กœ ์œ ์ง€ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์†์ƒ์‹œํ‚ค์ง€ ์•Š์œผ๋ฉด์„œ ์ง€์—ญ์  ์˜ˆ์ธก์„ ์ œ๊ณตํ•˜๋„๋ก WeatherKit๋Š” ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์œ„์น˜ ์ •๋ณด๋Š” ์˜ค์ง ์ผ๊ธฐ ์˜ˆ๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š”๋ฐ๋งŒ ์‚ฌ์šฉ๋˜๊ณ  ์ ˆ๋Œ€ ๊ณต์œ ๋˜๊ฑฐ๋‚˜ ํŒ๋งค๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Available Weather datasets

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-06-21 แ„‹แ…ฉแ„’แ…ฎ 3 22 54

Current weather

์š”์ฒญ๋œ ์œ„์น˜์˜ ํ˜„์žฌ ์กฐ๊ฑด์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

Minute forecast

๋‹ค์Œ ์‹œ๊ฐ„์— ๋Œ€ํ•œ ๋ถ„๋ณ„ ๊ฐ•์ˆ˜๋Ÿ‰ ์กฐ๊ฑด์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

Hourly forecast

ํ˜„์žฌ ์‹œ๊ฐ„๋ถ€ํ„ฐ ์‹œ์ž‘๋˜๋Š” ์˜ˆ๋ณด๋ฅผ ๋ชจ์•„ ๋†“์€ ๊ฒƒ์œผ๋กœ ์ตœ๋Œ€ 240์‹œ๊ฐ„ ๋™์•ˆ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ฐ„๋ณ„ ์˜ˆ๋ณด์˜ ๊ฐ ์‹œ๊ฐ„์—๋Š” ์Šต๋„, ๊ฐ€์‹œ์„ฑ, ๊ธฐ์•• ๋ฐ ์ด์Šฌ์ ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

Daily forecast

10์ผ ์˜ˆ์ธก ์ˆ˜์ง‘์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์ผ๋ณ„ ์˜ˆ๋ณด์˜ ๊ฐ ์š”์ผ์€ ๊ณ ์˜จ ๋ฐ ์ €์˜จ, ์ผ์ถœ ๋ฐ ์ผ๋ชฐ๊ณผ ๊ฐ™์€ ํ•˜๋ฃจ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

Weather alerts

์š”์ฒญ๋œ ์œ„์น˜์— ๋Œ€ํ•ด ๋ฐœํ–‰๋œ ์‹ฌ๊ฐํ•œ ๊ธฐ์ƒ ๊ฒฝ๋ณด๊ฐ€ ํฌํ•จ๋ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋ฅผ ์•ˆ์ „์„ ์œ„ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ ๋Œ€๋น„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

Historical weather

์ €์žฅ๋œ ๊ณผ๊ฑฐ ์ผ๊ธฐ ์˜ˆ๋ณด๋ฅผ ์ œ๊ณตํ•˜๋ฏ€๋กœ ๋‚ ์”จ ๋ฐ์ดํ„ฐ์˜ ์ถ”์„ธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Requesting weather

WeatherKit API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ ์”จ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…์‹œ๋‹ค.

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-06-21 แ„‹แ…ฉแ„’แ…ฎ 3 37 00
  1. ๊ธฐ๋ณธ ํ”„๋ ˆ์ž„์›Œํฌ ์‚ฌ์šฉ (Swift Framework)
  2. REST API ์„ธํŠธ ์‚ฌ์šฉ

Swift Framework ์‚ฌ์šฉ

๋ช‡ ์ค„๋งŒ ์žˆ์–ด๋„ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// Request the weather

import WeatherKit
import CoreLocation

// ๊ฐ์ฒด ์ƒ์„ฑ
let weatherService = WeatherService()
// ์œ„์น˜ ๊ฐ€์ ธ์˜ค๊ธฐ - ์ ‘๊ทผ ๊ถŒํ•œ ์š”์ฒญ ํ•„์š”
let syracuse = CLLocation(latitude: 43, longitude: -76)
// ์œ„์น˜ ์ •๋ณด๋กœ ๋‚ ์”จ ์ •๋ณด ์š”์ฒญ
let weather = try! await weatherService.weather(for: syracuse)
// 1. ์˜จ๋„ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผ
let temperature = weather.currentWeather.temperature
// 2. UV ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผ
let uvIndex = weather.currentWeather.uvIndex

REST API

Demo ํ”„๋กœ์ ํŠธ๋Š” ์—ฌ๊ธฐ์—์„œ ๋‹ค์šด๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

WeatherKit์„ ํ™œ์„ฑํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Demo Service

Filght Planner App

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-06-21 แ„‹แ…ฉแ„’แ…ฎ 4 00 28
  1. ๊ตฌ์กฐ์ฒด ๋งŒ๋“ค๊ธฐ (์ƒ๋žต)
  2. Shared Weather Service์—์„œ weather(for:)๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์‹œ๊ฐ„๋ณ„ ์˜ˆ๋ณด ์–ป๊ธฐ

Data Source์— ๋Œ€ํ•œ ์ €์ž‘๊ถŒ ํ‘œ์‹œ

๋‚ ์”จ ๋ฐ์ดํ„ฐ ์†Œ์Šค์— ๋Œ€ํ•œ ์ €์ž‘๊ถŒ ์ •๋ณด๊ฐ€ ํฌํ•จ๋œ Link(Apple๋กœ๊ณ  ํฌํ•จ)๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Light, Dark ๋ชจ๋“œ๋ฅผ ๋ชจ๋‘ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. colorScheme ํ™˜๊ฒฝ ๊ฐ’์„ ํ†ตํ•ด์„œ ํ‘œ์‹œํ•˜๋Š” ๊ฒƒ์„ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.

struct ContentView: View {
    var body: some View {
        .task {
            do {
                let attribution = try await WeatherService.shared.attribution
                attributionLink = attribution.legalPageURL
                attributionLogo = colorScheme == .light ? attribution.combineMarkDarkURL : attribution.combineMarkLightURL
            } catch {
                print(error)
            }
        }
    }
}

Apple weather mark ๋ฐ Attribution link๋Š” SFSafariViewController์—์„œ ์—ด๋ฆฝ๋‹ˆ๋‹ค.

์ง€๊ธˆ๊นŒ์ง€ ๋‹ค๋ฃฌ ๊ฒƒ์€ ๊ธฐ๋ณธ ํ”„๋ ˆ์ž„์›Œํฌ์— ๋ถˆ๊ณผํ•ฉ๋‹ˆ๋‹ค. REST API๋Š” Swift ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๋™์ผํ•œ ํ’๋ถ€ํ•œ ๋‚ ์”จ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜๊ณ , ๋ชจ๋“  ํ”Œ๋žซํผ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

REST API ์‚ฌ์šฉํ•˜๊ธฐ

weatherkit.apple.com ์—”๋“œํฌ์ธํŠธ์—์„œ ๋‚ ์”จ ์•Œ๋ฆผ์„ ์š”์ฒญํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…์‹œ๋‹ค.

/* Request a 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;

์œ„์˜ ์˜ˆ์‹œ๋Š” ์›น์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๋Š” ๊ณผ์ •์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์ธ์ฆ ํ† ํฐ์„ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.
  2. ๋‚ ์”จ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด URL์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. 2-1. ํ˜„์ง€ํ™”๋œ ์‘๋‹ต์— ์ ์ ˆํ•œ ์–ธ์–ด(๊ตญ๊ฐ€)๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 2-2. ๊ทธ๋ฆฌ๊ณ  ์ ์ ˆํ•œ ์œ„๋„์™€ ๊ฒฝ๋กœ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  3. URL๊ณผ ์ธ์ฆ ํ† ํฐ์„ ์‚ฌ์šฉํ•ด์„œ ๋‚ ์”จ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ JSON์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

REST API ์ธ์ฆ ์ฒ˜๋ฆฌ

WeatherKit REST API์˜ ๊ฒฝ์šฐ ์ธ์ฆ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ช‡ ๊ฐ€์ง€ ์ถ”๊ฐ€ ๋‹จ๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.