Skip to content

Fix OpenURL crash on macOS Sequoia #305

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
26 changes: 25 additions & 1 deletion Sources/Dependencies/DependencyValues/OpenURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,38 @@
}

@available(iOS 14, macOS 11, tvOS 14, watchOS 7, *)
private enum OpenURLKey: DependencyKey {
internal enum OpenURLKey: DependencyKey {
static let liveValue = OpenURLEffect { url in
let stream = AsyncStream<Bool> { continuation in
let task = Task { @MainActor in
#if os(watchOS)
EnvironmentValues().openURL(url)
continuation.yield(true)
continuation.finish()
#elseif os(macOS)
/// On macOS Sequoia, invokng `EnvironmentValues().openURL(_:completion:)`
/// i.e. with the completion handler, causes an `EXC_BREAKPOINT (SIGTRAP), KERN_INVALID_ADDRESS`
/// runtime crash with `_dispatch_assert_queue_fail` on a non-main thread.
if #available(macOS 15.0, *) {
let openURL = OpenURLAction { url in
EnvironmentValues().openURL(url)
/// However, using `.systemAction` (which may be needed to indicate whether an app
/// is available to handle a URL scheme), seems to cause the completion handler to never return...
/// Therefore, this implementation is actually equivalent to the watchOS one, which always
/// yields `true`.
return .handled
}

openURL(url) { canOpen in
continuation.yield(canOpen)
continuation.finish()
}
} else {
EnvironmentValues().openURL(url) { canOpen in
continuation.yield(canOpen)
continuation.finish()
}
}
#else
EnvironmentValues().openURL(url) { canOpen in
continuation.yield(canOpen)
Expand Down
18 changes: 18 additions & 0 deletions Tests/DependenciesTests/OpenURLTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import XCTest

@testable import Dependencies

class OpenURLTests: XCTestCase {
@Dependency(\.openURL) var openURL

/// Please note that running this test may cause side-effects, such as actually opening the
/// given webpage on the Mac that is running the test.
func testOpenURL_liveValue() async {
await withDependencies {
$0.openURL = OpenURLKey.liveValue
} operation: {
let url = URL(string: "https://www.pointfree.co/")!
await self.openURL(url)
}
}
}