Skip to content

Commit

Permalink
Refactor URL opening with shared function for iOS and macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
vtourraine committed Jan 18, 2024
1 parent 836c562 commit 0d852b4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
6 changes: 1 addition & 5 deletions Sources/AcknowList/AcknowListSwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 16 additions & 5 deletions Sources/AcknowList/AcknowListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

/**
Expand Down Expand Up @@ -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
}
}
4 changes: 1 addition & 3 deletions Sources/AcknowList/AcknowSwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand Down

0 comments on commit 0d852b4

Please sign in to comment.