ios - 找不到 WeatherKit 历史数据和归因问题

标签 ios xcode swiftui weather

我想将一些历史天气数据添加到应用程序中。我可以使用新的 WeatherKit 获取当前天气但找不到任何信息来告诉我如何 访问历史数据。 WWDC 视频之一提到了添加开始和结束 WeatherService 电话的日期,但我找不到这方面的任何信息。

此外,我还在努力满足归因要求。我可以让它工作,但只能在 灯光模式。当设备处于深色模式时,Apple Weather Logo 只是一个白色的 深色背景中的框(我假设 Logo 在那里,但为白色 - 但无法证明这一点)。

enter image description here

enter image description here

这是一个简化版本 - 仅获取当前天气:

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

任何指导将不胜感激。模拟器中的 Xcode 14.0 Beta、iOS 16.0 (20A5283p)

最佳答案

自 7 月 10 日起,提供的链接上的两个 Logo 均不可用。我现在在 AsyncImage 中创建了一个 placeholder,我不知道它是否会通过 Apple 的检查,但对于 Beta/离线解决方案来说似乎是可行的。

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()
}

历史天气现在可用

let forecast = try await weatherService.weather(for: location, including:.hourly(startDate: startDate, endDate: endDate))

let forecast = try await weatherService.weather(for: location, including: .daily(startDate: startDate, endDate: endDate))

关于ios - 找不到 WeatherKit 历史数据和归因问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72581627/

相关文章:

android - Xamarin 是一个很好的跨平台库,可以从 Android 开始,然后再转向 iOS

ios - 使用参数生成 NSString 时在 Release模式下崩溃

ios - Pod 配置后的 Xcode 7.3 : error: could not read CFBundleIdentifier from Info. plist (null)

swift - SwiftUI 列表禁用滚动

SwiftUI - 从列表中删除项目崩溃并出现 fatal error : Index out of range

ios - 删除 SwiftUI (WatchKit) 中的圆形按钮背景

ios - 创建复杂的 [String :AnyObject] for Alamofire parameters

IOS 上一个/下一个按钮意外关闭键盘

ios - 如何将 TextView 的文本设置为通过从应用程序操作扩展接收文本创建的字符串

iphone - 为什么我的对象没有通过 segue 正确传递?