I'm getting my feet wet with weatherKit and have managed to get all the current weather I need with the code below.
My app requires one more thing - the chance of precipitation in the next 0-20 minutes or so - in order to close the windows if the odds exceed some threshold.
I haven't been able to find a single code example of retrieving the forecasted rain odds. It looks like the number I need will be in the MinuteWeather contained? in the HourlyForecast but it's completely a mystery to me how to extract what I need. I need a trail of breadcrumbs!
I tried getting the hourly and daily forecasts but the value I'm looking for isn't there.
import Foundation import WeatherKit @Observable class WeatherManager { private let weatherService = WeatherService() var weather: Weather? func getWeather(lat: Double, long: Double) async { do { weather = try await Task.detached(priority: .userInitiated) { [weak self] in return try await self?.weatherService.weather(for: .init(latitude: lat, longitude: long)) }.value } catch { print("Failed to get weather data. \(error)") } print("\(weather)") do { let daily = try await weatherService.weather(for: .init(latitude: lat, longitude: long), including: .daily) let hourly = try await weatherService.weather(for: .init(latitude: lat, longitude: long), including: .hourly) print("Daily: \(daily)") print("Hourly: \(hourly)") } catch let error { print("Failed to get the forecast. \(error)") } } // close getWeather function var icon: String { guard let iconName = weather?.currentWeather.symbolName else { return "--" } return iconName } var temperature: String { guard let temp = weather?.currentWeather.temperature else { return "--" } let convert = temp.converted(to: .fahrenheit).value return String(Int(convert)) + "°F" } var humidity: String { guard let humidity = weather?.currentWeather.humidity else { return "--" } let computedHumidity = humidity * 100 return String(Int(computedHumidity)) + "%" } var pressure: String { guard let press = weather?.currentWeather.pressure else { return "--" } let convertP = press.converted(to: UnitPressure.inchesOfMercury).value return String((convertP)) + " in. Hg" } var UVindex: String { guard let uv = weather?.currentWeather.uvIndex.value else { return "--" } return "\(uv)" + " index" } var POP: String { ???? return String(Int(chance)) + "%" } }