From 0d852b42d3927546193e8559a9a2dffd13b90487 Mon Sep 17 00:00:00 2001 From: Vincent Tourraine Date: Thu, 18 Jan 2024 11:12:34 +0100 Subject: [PATCH] Refactor URL opening with shared function for iOS and macOS --- Sources/AcknowList/AcknowListSwiftUI.swift | 6 +----- .../AcknowList/AcknowListViewController.swift | 21 ++++++++++++++----- Sources/AcknowList/AcknowSwiftUI.swift | 4 +--- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Sources/AcknowList/AcknowListSwiftUI.swift b/Sources/AcknowList/AcknowListSwiftUI.swift index 15d4992..15b7b18 100644 --- a/Sources/AcknowList/AcknowListSwiftUI.swift +++ b/Sources/AcknowList/AcknowListSwiftUI.swift @@ -129,11 +129,7 @@ public struct AcknowListRowSwiftUIView: View { else if let repository = acknowledgement.repository, canOpenRepository(for: repository) { Button(action: { -#if os(macOS) - NSWorkspace.shared.open(repository) -#elseif os(iOS) - UIApplication.shared.open(repository) -#endif + repository.openWithDefaultBrowser() }) { Text(acknowledgement.title) .foregroundColor(.primary) diff --git a/Sources/AcknowList/AcknowListViewController.swift b/Sources/AcknowList/AcknowListViewController.swift index 7b84dd1..ba2eb43 100644 --- a/Sources/AcknowList/AcknowListViewController.swift +++ b/Sources/AcknowList/AcknowListViewController.swift @@ -217,11 +217,7 @@ open class AcknowListViewController: UITableViewController { return } - if #available(iOS 10.0, tvOS 10.0, *) { - UIApplication.shared.open(url, options: [:], completionHandler: nil) - } else { - UIApplication.shared.openURL(url) - } + url.openWithDefaultBrowser() } /** @@ -460,3 +456,18 @@ open class AcknowListViewController: UITableViewController { } #endif + +internal extension URL { + func openWithDefaultBrowser() { +#if os(macOS) + NSWorkspace.shared.open(self) +#elseif os(iOS) + if #available(iOS 10.0, *) { + UIApplication.shared.open(self) + } + else { + UIApplication.shared.openURL(self) + } +#endif + } +} diff --git a/Sources/AcknowList/AcknowSwiftUI.swift b/Sources/AcknowList/AcknowSwiftUI.swift index c625fe1..db9c9a7 100644 --- a/Sources/AcknowList/AcknowSwiftUI.swift +++ b/Sources/AcknowList/AcknowSwiftUI.swift @@ -69,9 +69,7 @@ public struct AcknowSwiftUIView: View { acknowledgement = Acknow(title: acknowledgement.title, text: text, license: acknowledgement.license, repository: acknowledgement.repository) case .failure: -#if os(iOS) - UIApplication.shared.open(repository) -#endif + repository.openWithDefaultBrowser() } } }