Use WeatherKit in an Xcode playground?

Can it be done?

I can reliably crash Xcode by attempting the following in a playground:

import UIKit
import WeatherKit
import CoreLocation
let sanFrancisco = CLLocation(latitude: 37.7749, longitude: 122.4194)
let weatherService = WeatherService()

let weather = try await weatherService.weather(for: sanFrancisco)
let temperature = weather.currentWeather.temperature
print(temperature)

Xcode 14.3.1 macOS 13.4.1

Post not yet marked as solved Up vote post of waynehend Down vote post of waynehend
895 views

Replies

This should work , it's maybe an Xcode issue

Does that code not crash your Xcode? Again, it's in a playground.

Update: This code does not crash the app, and the error returned hints that I might have an access issue. I'm still wondering if this is even possible in a playground.

As an aside, I have successfully used Apple's Flight Planner app that is offered as a WeatherKit demo. So my account seems to be "authorized" or whatever the word is.

import UIKit
import WeatherKit
import CoreLocation

let sanFrancisco = CLLocation(latitude: 37.7749, longitude: 122.4194)

do {
    let weather = try await WeatherService().weather(for: sanFrancisco)
} catch {
    print(error)
}

This produces the error:

xpcConnectionFailed(Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.weatherkit.authservice" UserInfo={NSDebugDescription=connection to service named com.apple.weatherkit.authservice})
Playground execution failed:

error: execution stopped with unexpected state.
error: Execution was interrupted.
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

If you use Xcode to create an app project and then put similar code in that, does it work there?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I'm having a harder time trying that than you'd expect. Getting it to work in an app was what I was trying to avoid by using the playground to get my code working first.

Does your question/answer imply that it SHOULD be working in a playground? I mean, does that code work for you?

Another clue? Following the example at https://blog.makwanbk.com/weatherkit-build-a-simple-ios-weather-app, I put the following code in my playground and got the error below. It's pretty much the same as before but now expressed in a localized description. I was looking at my developer account and discovered I have no current provisioning profiles. Would that do it? As I said the Airport Forecast example app works in Simulator

import UIKit
import WeatherKit
import CoreLocation

    let service = WeatherService()
    let currentLocation = CLLocation(latitude: 37.7749, longitude: 122.4194)
            do {
                let weather = try await service.weather(for: currentLocation)
                print(weather)
                let currentWeather = Weather(temperature: weather.currentWeather.temperature.value,
                                              condition: weather.currentWeather.condition.rawValue,
                                              symbolName: weather.currentWeather.symbolName,
                                              humidity: weather.currentWeather.humidity,
                                              isDaylight: weather.currentWeather.isDaylight)
                print(currentWeather)
            } catch {
                assertionFailure(error.localizedDescription)
            }

struct Weather {
    let temperature: Double
    let condition: String
    let symbolName: String
    let humidity: Double
    let isDaylight: Bool
    
    static func empty() -> Weather {
        Weather(temperature: 0,
                condition: "",
                symbolName: "",
                humidity: 0,
                isDaylight: false)
    }
}
__lldb_expr_17/Weather.playground:31: Fatal error: The operation couldn’t be completed. (WeatherDaemon.WDSJWTAuthenticatorServiceProxy.Errors error 0.)

Does your question/answer imply that it SHOULD be working in a playground?

I asked because it’s a useful diagnostic test.

WeatherKit requires an entitlement. Mixing entitlements and playgrounds is always confusing (sometimes the playground gets more entitlements than you expect, and sometimes fewer). In contrast, an Xcode app project has a clear way to control the entitlements you get — in this case using the WeatherKit capability in the Signing & Capabilities editor — and that’s why I recommended this test.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Tests complete. I can successfully use the FlightPlanner demo or a simpler project from here: https://github.com/coledennis/WeatherKit_CoreLocation_SwiftUI_Tutorial

... (before and) after changing the bundle ID to my own, one which I have registered for WeatherKit. This was not the case when I started, though.

But, a playground with any of the code samples above still throws the same errors as before.

I can abandon trying to use Playground to learn my way around WeatherKit. I'll switch to using demo apps. I thought it would make things easier and faster to use a Playground but that's obviously not the case.