Open Taehyeon-Kim opened 2 years ago
๋ ์จ ์ ๋ณด์ ์ค์์ฑ
์ฐ๋ฆฌ๋ ๋ ์จ ๋ฐ์ดํฐ์ ์์กดํ๋ฉฐ ์ฝ๋๋ค. ์ํฉ์ ํ ๋ฒ ๊ฐ์ ํด๋ณด๊ฒ ์ต๋๋ค.
์์์ ์ธ๊ธํ ์ฌ๋ฌ ๊ฐ์ง ์ํฉ์ ์ด๋ฅด๊ธฐ๊น์ง ๋ ์จ๋ ๋ชจ๋ ์ฌ๋์๊ฒ ์ํฅ์ ๋ฏธ์น๋๋ฐ์.์ด์ ๋ฐ๋ผ์ ์ ํํ ๊ธฐ์ ๋ฐ์ดํฐ๋ ๋ณํํ๋ ๊ธฐํ์ ์ํฅ์ ๋ง์ด ๋ฐ๋ ์ค๋๋ ์ ์ธ๊ณ์ ๋์ฑ ์ค์ํด์ก์ต๋๋ค.
๊ทธ๋์ WeatherKit์ ๋ง๋ค์์ต๋๋ค.
์์ ๊ธฐ์ ์ ์ฌ์ฉํ์ฌ ์ ์ธ๊ณ์ ์ด์ง์ญ ๊ธฐ์ ์๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค. Apple Weather Service๋ฅผ ํตํด์ ๋ง์ ๋ฐ์ดํฐ์ ์ ๊ทผํ ์ ์์ต๋๋ค.
์ ํํ ๋ ์จ ๋ฐ์ดํฐ๋ ์์น ์ ๋ณด๊ฐ ํ์ํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ ๋ฐ์ดํฐ๋ฅผ ๋น๊ณต๊ฐ๋ก ์ ์งํ๋๋ก ํฉ๋๋ค. ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ์์์ํค์ง ์์ผ๋ฉด์ ์ง์ญ์ ์์ธก์ ์ ๊ณตํ๋๋ก WeatherKit๋ ์ค๊ณ๋์์ต๋๋ค. ์์น ์ ๋ณด๋ ์ค์ง ์ผ๊ธฐ ์๋ณด๋ฅผ ์ ๊ณตํ๋๋ฐ๋ง ์ฌ์ฉ๋๊ณ ์ ๋ ๊ณต์ ๋๊ฑฐ๋ ํ๋งค๋์ง ์์ต๋๋ค.
์์ฒญ๋ ์์น์ ํ์ฌ ์กฐ๊ฑด์ ์ค๋ช ํฉ๋๋ค.
๋ค์ ์๊ฐ์ ๋ํ ๋ถ๋ณ ๊ฐ์๋ ์กฐ๊ฑด์ด ํฌํจ๋ฉ๋๋ค.
ํ์ฌ ์๊ฐ๋ถํฐ ์์๋๋ ์๋ณด๋ฅผ ๋ชจ์ ๋์ ๊ฒ์ผ๋ก ์ต๋ 240์๊ฐ ๋์์ ๋ฐ์ดํฐ๋ฅผ ์ ๊ณตํฉ๋๋ค. ์๊ฐ๋ณ ์๋ณด์ ๊ฐ ์๊ฐ์๋ ์ต๋, ๊ฐ์์ฑ, ๊ธฐ์ ๋ฐ ์ด์ฌ์ ๊ณผ ๊ฐ์ ์กฐ๊ฑด์ด ํฌํจ๋ฉ๋๋ค.
10์ผ ์์ธก ์์ง์ด ํฌํจ๋ฉ๋๋ค. ์ผ๋ณ ์๋ณด์ ๊ฐ ์์ผ์ ๊ณ ์จ ๋ฐ ์ ์จ, ์ผ์ถ ๋ฐ ์ผ๋ชฐ๊ณผ ๊ฐ์ ํ๋ฃจ์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณตํฉ๋๋ค.
์์ฒญ๋ ์์น์ ๋ํด ๋ฐํ๋ ์ฌ๊ฐํ ๊ธฐ์ ๊ฒฝ๋ณด๊ฐ ํฌํจ๋ฉ๋๋ค. ์ฌ์ฉ์๋ฅผ ์์ ์ ์ํ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ฉฐ ๋๋นํ ์ ์๋๋ก ํฉ๋๋ค.
์ ์ฅ๋ ๊ณผ๊ฑฐ ์ผ๊ธฐ ์๋ณด๋ฅผ ์ ๊ณตํ๋ฏ๋ก ๋ ์จ ๋ฐ์ดํฐ์ ์ถ์ธ๋ฅผ ๋ณผ ์ ์์ต๋๋ค.
WeatherKit API๋ฅผ ์ฌ์ฉํ์ฌ ๋ ์จ ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๋ ๋ฐฉ๋ฒ์ ์์๋ด ์๋ค.
๋ช ์ค๋ง ์์ด๋ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
// 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
Demo ํ๋ก์ ํธ๋ ์ฌ๊ธฐ์์ ๋ค์ด๋ก๋ ํ ์ ์์ต๋๋ค.
WeatherKit์ ํ์ฑํํด์ผ ํฉ๋๋ค.
Filght Planner App
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 ํ๋ ์์ํฌ์ ๋์ผํ ํ๋ถํ ๋ ์จ ๋ฐ์ดํฐ๋ฅผ ์ ๊ณตํ๊ณ , ๋ชจ๋ ํ๋ซํผ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
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;
์์ ์์๋ ์น์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ ๊ณผ์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค.
WeatherKit REST API์ ๊ฒฝ์ฐ ์ธ์ฆ์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ๋ช ๊ฐ์ง ์ถ๊ฐ ๋จ๊ณ๊ฐ ์์ต๋๋ค.