调用WeatherKit获取天气数据时出现WeatherDataService错误求助
WeatherKit报错:Response did not contain requested dataset: .appLocationConfig 解决方法
报错日志
2022-11-15 15:26:29.810771+0900 WeatherApp[40976:2003546] [WeatherDataService] Response did not contain requested dataset: .appLocationConfig. Returning default value: AppLocationConfig(reportAnIssue: ReportIssueLocationConfig(position: beforeFooter, minDaysSinceInteraction: 0), expireTime: 2022-11-15 06:56:29 +0000
实现代码
func getWeather(CLlocation: CLLocation, completion: @escaping () -> Void){ Task { do { let result = try await weatherService.weather(for: CLlocation) weatherDataArray.append(result) } catch { print(error) } } } func getWeatherWithCood(lat: CLLocationDegrees, lon: CLLocationDegrees, completion: @escaping () -> Void){ Task { do { let result = try await weatherService.weather(for: .init(latitude: lat, longitude: lon)) weatherDataArray.append(result) } catch { print(error) } } }
已检查账户标识符,问题仍存在,求解决。
解决步骤
1. 显式指定所需数据集
默认调用weather(for:)会请求所有可用数据集,其中包含非核心的appLocationConfig。通过显式指定需要的数据集,可避免请求该配置项:
// 修改后的getWeather方法示例:仅请求当前天气和每日预报 func getWeather(CLlocation: CLLocation, completion: @escaping () -> Void){ Task { do { // 指定需要的数据集,比如.current(当前天气)、.forecastDaily(每日预报)等 let result = try await weatherService.weather(for: CLlocation, including: [.current, .forecastDaily]) weatherDataArray.append(result) } catch { print(error) } } }
同理修改getWeatherWithCood方法即可。
2. 验证服务配置完整性
- 检查
Info.plist是否添加NSWeatherUsageDescription键,且值不为空(用于向用户说明天气数据使用目的)。 - 确认Xcode项目的Signing & Capabilities中已添加WeatherKit能力,且开发者账号已开通该服务。
- 确保设备网络正常,能访问Apple的WeatherKit服务端点。
3. 忽略非核心提示日志
如果核心天气数据能正常获取,该日志仅为系统返回默认配置的提示,不影响功能运行,可直接忽略。
内容的提问来源于stack exchange,提问作者Pulsar
相关产品推荐
相关产品推荐

