From e52c1ca5b355bc384ea74dc97fa0303f608a7485 Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 28 Nov 2023 03:31:40 +0800 Subject: [PATCH] Add macOS openURL implementation and fix test crash --- .../EnvironmentValues/EnvironmentValues+openURL.swift | 8 ++++++-- .../EnvironmentValues/EnvironmentValuesOpenURLTests.swift | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Sources/OpenSwiftUI/DataAndStorage/EnvironmentValues/EnvironmentValues+openURL.swift b/Sources/OpenSwiftUI/DataAndStorage/EnvironmentValues/EnvironmentValues+openURL.swift index 050d19ba..2ae6368e 100644 --- a/Sources/OpenSwiftUI/DataAndStorage/EnvironmentValues/EnvironmentValues+openURL.swift +++ b/Sources/OpenSwiftUI/DataAndStorage/EnvironmentValues/EnvironmentValues+openURL.swift @@ -13,6 +13,8 @@ import UIKit private import UIKitCore private import CoreServices #endif +#elseif os(macOS) +import AppKit #endif #endif @@ -97,10 +99,12 @@ extension EnvironmentValues { struct OpenURLActionKey: EnvironmentKey { static let defaultValue = OpenURLAction( handler: .system { url, completion in - #if os(iOS) || os(tvOS) || os(visionOS) + #if os(iOS) || os(tvOS) UIApplication.shared.open(url, options: [:], completionHandler: completion) #elseif os(macOS) - fatalError("Unimplemented") + NSWorkspace.shared.open(url, configuration: .init()) { application, error in + completion(error != nil) + } #else fatalError("Unimplemented") #endif diff --git a/Tests/OpenSwiftUITests/DataAndStorage/EnvironmentValues/EnvironmentValuesOpenURLTests.swift b/Tests/OpenSwiftUITests/DataAndStorage/EnvironmentValues/EnvironmentValuesOpenURLTests.swift index 39b6bc44..3c884aab 100644 --- a/Tests/OpenSwiftUITests/DataAndStorage/EnvironmentValues/EnvironmentValuesOpenURLTests.swift +++ b/Tests/OpenSwiftUITests/DataAndStorage/EnvironmentValues/EnvironmentValuesOpenURLTests.swift @@ -9,9 +9,9 @@ import XCTest @testable import OpenSwiftUI final class EnvironmentValuesOpenURLTests: XCTestCase { - #if DEBUG - func testOpenSensitiveURLActionKey() throws { - let value = OpenSensitiveURLActionKey.defaultValue + #if os(iOS) || os(macOS) || os(tvOS) + func testOpenURLActionKey() { + let value = OpenURLActionKey.defaultValue value.callAsFunction(URL(string: "https://example.com")!) } #endif