You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

获取当前位置天气时JWT Token生成失败问题求助

问题:Weather Kit JWT Token生成失败错误(Code=2)

尝试获取当前位置天气信息时出现如下错误:

Failed to generate jwt token for: com.apple.weatherkit.authservice with error: Error Domain=WeatherDaemon.WDSJWTAuthenticatorServiceListener.Errors Code=2 "(null)"

相关代码如下:

LocationManager 实现

class LocationManager: NSObject, ObservableObject{

    @Published var currentLocation: CLLocation?
    private let locationManager = CLLocationManager()

    override init(){
        super.init()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
        locationManager.delegate = self

    }
}
extension LocationManager: CLLocationManagerDelegate{
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        guard let location = locations.last, currentLocation == nil else {return}

        DispatchQueue.main.async {
            self.currentLocation = location
        }
    }
    func locationManager(_ manager: CLLocationManager,
                     didFailWithError error: Error) {
        print( "location manager failed with error \(error)" )
    }
}

MainView 实现

struct MainView: View {

    let weatherService = WeatherService.shared
    @StateObject private var locationManager = LocationManager()

    var body: some View {
        VStack{
            Text("Hello, World!")
        }.task(id: locationManager.currentLocation){
            do{
                if let location = locationManager.currentLocation{
                    let weather = try await weatherService.weather(for: location)
                    print(weather)
                }
            }catch{
                print(error)
            }
        }

    }
}

已在App信息和开发者后台添加了Weather Kit权限,请问该问题的原因是什么?


可能的原因及排查方向
  • 签名密钥配置不完整:
    仅添加Weather Kit权限不够,需在Apple开发者后台生成并下载专属的Weather Kit签名密钥,同时在项目中配置密钥ID(Key ID)、团队ID(Team ID)、Bundle ID等核心参数,确保WeatherService初始化时能读取这些信息生成合法的JWT。

  • 位置权限描述缺失:
    代码中请求了Always位置权限,但Info.plist中必须添加NSLocationAlwaysAndWhenInUseUsageDescription或NSLocationWhenInUseUsageDescription的描述文本。无合法描述会导致系统拒绝授予位置权限,间接引发Weather Kit请求失败。

  • 环境与设备限制:
    Weather Kit在模拟器上可能存在JWT验证异常,建议切换到真实iOS设备测试;同时确认开发者账号已激活Weather Kit服务,且项目Bundle ID与开发者后台配置完全一致。

  • JWT生成参数错误:
    检查WeatherService的JWT生成逻辑,确保使用ES256算法、密钥内容正确、token过期时间不超过24小时,且issuer为团队ID、audience为https://weatherkit.apple.com,任何参数不符合要求都会导致token生成失败。


内容的提问来源于stack exchange,提问作者Nat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 07:31:07