WeatherKit Gives 404

When I'm using the following code into the simulator it gives the wanted result:

Button {

                Task {

                    localWeather = try await WeatherService.shared.weather(for: CLLocation(latitude: 52.5153, longitude: 6.08565), including:  .daily(startDate: Date(), endDate: Calendar.current.date(byAdding: .day, value: 1, to: Date())!)).first.debugDescription

                }

            } label: {

                Text("Get the test weather")

            }

            Text(localWeather)

When I'm using this exact code on a real device I get the following 404 error:

[WeatherDataService] Received invalid http response code 404 for request: C7AEC7CC-E5F7-425D-8491-25B9302E2A0F:0

[WeatherService] Encountered an error when fetching weather data subset; location=<+52.51530000,+6.08565000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 05/01/2023, 13:55:53 Central European Standard Time,  error=responseFailed(<NSHTTPURLResponse: 0x282ce0260> { URL: https://weather-data.apple.com/v3/weather/en/52.515/6.086?timezone=Europe/Amsterdam&dataSets=forecastHourly,forecastDaily&dailyStart=2023-01-10T12:00:00Z&dailyEnd=2023-01-10T12:00:00Z&hourlyStart=2023-01-10T11:00:00Z&hourlyEnd=2023-01-10T16:00:00Z&country=NL } { Status Code: 404, Headers {

    "Access-Control-Allow-Origin" =     (

        "*"

    );

    "Cache-Control" =     (

        "max-age=0, no-cache, no-store"

    );

    Connection =     (

        "keep-alive"

    );

    "Content-Length" =     (

        0

    );

    "Content-Security-Policy" =     (

        "default-src 'self';"

    );

    Date =     (

        "Thu, 05 Jan 2023 12:59:27 GMT"

    );

    Expires =     (

        "Thu, 05 Jan 2023 12:59:27 GMT"

    );

    Pragma =     (

        "no-cache"

    );

    Server =     (

        "AppleHttpServer/21be5247c6351682d1d9aa22fe98c8f0d4902838"

    );

    "Strict-Transport-Security" =     (

        "max-age=31536000; includeSubDomains",

        "max-age=31536000"

    );

    "X-Apple-Origin" =     (

        "bcd49c7f-c567-3921-a041-3d4ef58e5423"

    );

    "X-B3-TraceId" =     (

        c8c6547722afc53f

    );

    "X-Cache" =     (

        "TCP_MISS from a104-110-190-91.deploy.akamaitechnologies.com (AkamaiGHost/10.10.3-45298580) (-)"

    );

    "X-Content-Type-Options" =     (

        nosniff

    );

    "X-Frame-Options" =     (

        DENY

    );

    "X-REQUEST-ID" =     (

        "407e37bc-ddf6-45fd-84d0-1d3d3b8651b0"

    );

    "X-XSS-Protection" =     (

        "1; mode=block"

    );

} }, Optional(""))

I fixed it once by deleting Xcode and the app from my iPhone, but that doesn't work anymore.

Any suggestions?

Post not yet marked as solved Up vote post of Jeroeneo Down vote post of Jeroeneo
1.3k views

Replies

Have you found an answer/solution? I'm facing the exact same issue...

Not sure if you found a solution, but here's what my problem was.

I was sending the same date for startDate and endDate, which returned a 404. Make sure that you are sending a valid startDate and endDate

Hope this helps somebody else that stumbles across this thread.

I've see this before and in my case it was because Date() returns the date at UTC and NOT the local date. So, depending on the time of day that the Date() function is called and where the device is located, you might get a date result that is NOT the local date of the device! It took me a while to figure this one out. To avoid problems with this, you would need to pass the local date to WeatherKit if you want to startDate to be the current date at the device's location. Luckily, there is a nice extension on StackOverflow that gives you a Date().localDate() function that fixes this issue. https://stackoverflow.com/questions/28404154/swift-get-local-date-and-time