Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watchOS: fixed crash on single-target apps #1849

Merged
merged 2 commits into from
Aug 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions Sources/Misc/SystemInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,12 @@ private extension SystemInfo {
#elseif os(macOS)
return false
#elseif os(watchOS)
return WKExtension.shared().applicationState == WKApplicationState.background
return self.isApplicationBackgroundedWatchOS
#endif
}

#if os(iOS) || os(tvOS)

// iOS/tvOS App extensions can't access UIApplication.sharedApplication, and will fail to compile if any calls to
// it are made. There are no pre-processor macros available to check if the code is running in an app extension,
// so we check if we're running in an app extension at runtime, and if not, we use KVC to call sharedApplication.
Expand All @@ -211,7 +212,30 @@ private extension SystemInfo {
}

guard let sharedUIApplication = self.sharedUIApplication else { return false }
return sharedUIApplication.applicationState == UIApplication.State.background
return sharedUIApplication.applicationState == .background
}

#elseif os(watchOS)

var isApplicationBackgroundedWatchOS: Bool {
// In Xcode 13 and earlier the system divides a watchOS app into two sections:
// - WatchKit app
// - WatchKit extension
// In Xcode 14 and later, you can produce watchOS apps with a single watchOS app target.
// These single-target watchOS apps can run on watchOS 7 and later.
// Calling `WKExtension.shared` on these single-target apps crashes.
//
// `WKApplication` provides a more accurate value if it's available, and it avoids that crash.
#if swift(>=5.7)
if #available(watchOS 7.0, *) {
return WKApplication.shared().applicationState == .background
} else {
return WKExtension.shared().applicationState == .background
}
#else
// Before Xcode 14, single-target extensions aren't supported (and WKApplication isn't available)
return WKExtension.shared().applicationState == .background
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this becomes slightly more complex, having to check for 2 things... But well, I don't have a better way to do it and it's not too bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but moving forward we'll only have one. Once Xcode 14 is mandatory, and when we drop anything before watchOS 7.0, there's really only one implementation :)

}

#endif
Expand Down