I would like to add some historical weather data to an app. I am able to use the new WeatherKit to get current weather but cannot find ANY information to tell me how to access historical data. One of the WWDC videos made reference to adding a start and end date to the WeatherService call but I cannot find any info on this.
Also, I am struggling with the attribution requirements. I can make it work but only in light mode. When the device is in dark mode, the Apple Weather Logo is just a white box in the dark background (I assume the logo is there but in white - but can't prove it).
This is a simplified version - fetching current weather only:
struct ContentView: View {
@Environment(\.colorScheme) var colorScheme
@State private var weather: Weather?
@State private var attLogo: URL?
@State private var attributionURL: URL?
@State private var logoImage: Image?
let weatherService = WeatherService.shared
var body: some View {
VStack {
if let weather {
VStack {
Text("San Francisco")
.font(.largeTitle)
Text("\(weather.currentWeather.temperature.formatted()) | \(weather.currentWeather.condition.description)")
}
}//if let
Spacer()
//white letters on white box if device in dark mode
AsyncImage(url: attLogo)
Group{
if let attributionURL {
Link("Weather Attribution", destination: attributionURL)
}
}//att group
}//outer v
.padding()
.task {
do {
let location = CLLocation(latitude: 37.77, longitude: -122.41)
self.weather = try await weatherService.weather(for: location)
} catch {
print(error)
}//do catch
}//task 1
.task {
do {
let attribution = try await weatherService.attribution
let attributionLink = attribution.legalPageURL
self.attributionURL = attributionLink
let attributionLogo = colorScheme == .light ? attribution.combinedMarkDarkURL : attribution.combinedMarkLightURL
self.attLogo = attributionLogo
} catch {
print("failed to load attribution")
}
}//task for logo and link
}//body
}//struct
Any guidance would be appreciated. Xcode 14.0 Beta, iOS 16.0 (20A5283p) in Simulator
As of 10 July both logo marks are unavailable at the provided links. I have created a placeholder
in the AsyncImage
for now, I don't know if it would ever pass Apple's check but it seems viable for a Beta/offline solution.
if let arributionLogo = arributionLogo{
AsyncImage(url: arributionLogo) { image in
image.scaledToFit()
} placeholder: {
Label("Apple Weather", systemImage: "cloud.sun.fill")
}
}else{
ProgressView()
}
if let arributionLink = arributionLink{
Link("Other data sources", destination: arributionLink)
}else{
ProgressView()
}
Historical Weather is now available with
let forecast = try await weatherService.weather(for: location, including:.hourly(startDate: startDate, endDate: endDate))
And
let forecast = try await weatherService.weather(for: location, including: .daily(startDate: startDate, endDate: endDate))