From f0ca714c25a77e8f08125f14fa5a2dd51da65c85 Mon Sep 17 00:00:00 2001 From: Pete Schwamb Date: Sat, 31 Oct 2026 14:40:53 -0500 Subject: [PATCH] Fix watch crash when changing pages ExtensionDelegate.shared() looked up WKApplication.shared().delegate, which is nil under the SwiftUI app lifecycle, so it trapped. The usage recording added in #2629 calls it from SwiftUI views, so swiping between pages, or opening carbs, bolus or presets, crashed the watch app. The delegate now records itself when it is created. --- WatchApp Extension/ExtensionDelegate.swift | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/WatchApp Extension/ExtensionDelegate.swift b/WatchApp Extension/ExtensionDelegate.swift index dfd51a8510..ea1f3ac607 100644 --- a/WatchApp Extension/ExtensionDelegate.swift +++ b/WatchApp Extension/ExtensionDelegate.swift @@ -25,8 +25,11 @@ class ExtensionDelegate: NSObject, WKApplicationDelegate { private var observers: [NSKeyValueObservation] = [] private var notifications: [NSObjectProtocol] = [] + // WKApplication.shared().delegate is nil under the SwiftUI app lifecycle. + private static var instance: ExtensionDelegate! + static func shared() -> ExtensionDelegate { - return WKApplication.shared().extensionDelegate + return instance } let loopManager = LoopDataManager.shared @@ -42,6 +45,8 @@ class ExtensionDelegate: NSObject, WKApplicationDelegate { override init() { super.init() + Self.instance = self + let session = WCSession.default session.delegate = self @@ -422,10 +427,3 @@ extension ExtensionDelegate { WKApplication.shared().rootInterfaceController?.presentAlert(withTitle: error.localizedDescription, message: (error as NSError).localizedRecoverySuggestion ?? (error as NSError).localizedFailureReason, preferredStyle: .alert, actions: [WKAlertAction.dismissAction()]) } } - - -fileprivate extension WKApplication { - var extensionDelegate: ExtensionDelegate! { - return delegate as? ExtensionDelegate - } -}