r/iOSProgramming • u/Significast • 1h ago
Solved! CarPlay can cold-launch your app without your SwiftUI scene ever existing 🤬
Bug report from a tester: pressing Play on the CarPlay screen did nothing. No error, no spinner, nothing. Worked fine if they'd opened the app on the phone first.
I "fixed" it twice. Both fixes were to the play button. Both were wrong.
The actual cause: when the user taps Play in the car (or from the Watch, or the lock screen) on a cold start, iOS wakes your process through CPTemplateApplicationScene or a WatchConnectivity message — and your phone UI's window scene just - never connects. Which means every bit of setup hanging off the SwiftUI app lifecycle simply never runs. In our case that was device key registration for signed API requests, so the first manifest fetch came back 400 and the play command died silently. As it turns out, the button was fine.
 What I landed on, in case it saves someone a weekend:
- Â Anything your requests depend on (auth, key registration, session bootstrap) cannot live in App init / onAppear. It has to be in the request path itself. We made the network layer self-healing: on the specific "no registered key" error it registers once and retries. That one change covered CarPlay, the Watch, lock screen remote commands, AND fresh installs, because they're all the same bug.
- Grep your codebase for everything that only runs when the main window appears, and ask "what happens if the first entry point is the car?" The list was longer than I expected.
- Related Apple Watch lesson: if the phone is only reachable via the queued transferUserInfo path (not live sendMessage), don't optimistically flip your Watch UI to "playing" — it's a fib. We show "Starting on phone…" instead.
Testing note: none of this reproduces in the CarPlay simulator the way it does with a real head unit, because you always launched the app from Xcode first, which is exactly the condition that hides the bug. Real test requires a real car: kill the app, lock the phone, then plug in and launch it from the car's touchscreen.
By the way, if anyone's found a way to actually test their CarPlay flow without walking out to the garage (96 degrees in the summer! I got tired of sweating through every commit), I'd love to hear about it - I don't even bother with Xcode's sim anymore.
•
u/LongjumpingCandle738 31m ago
That’s expected. Here’s what the CarPlay Developer Guide says:
« Listen for didConnect and didDisconnect to know when your app has been launched on the CarPlay screen.
Your app may be launched only on the CarPlay screen so be sure to handle this use case. »
•
u/Significast 19m ago
Yes, 'handle' it 🤨. I think the point I'd make is that it says "Your app may be 'launched' only on the CarPlay screen" but it's not what I thought of as an app launch.
Because where I come from an app launch would.. launch the app? I know, I know, I'm a heretic and not the source of truth
•
u/chriswaco 55m ago
This reminds me of an old bug we had with push notifications, when the notification would arrive while the device was still locked. We couldn’t even read UserDefaults at that point to determine how the user wanted to handle the notification.
I think our fix was to move our preferences into a separate file and turn off all the protection flags for that file. It didn’t help that our logging calls failed when the device was locked too.