[CAKER-12] Adding open(URL) capability for macOS (#3)

Work on xiiagency/Caker#12
This commit is contained in:
Gennadiy Shafranovich 2022-02-04 11:08:07 -05:00 committed by GitHub
parent 3a5f6bf664
commit c08716eef2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 27 deletions

View File

@ -15,11 +15,15 @@ let package =
targets: ["SwiftUIExtensions"]
),
],
dependencies: [],
dependencies: [
.package(name: "SwiftFoundationExtensions", url: "https://github.com/xiiagency/SwiftFoundationExtensions", .branchItem("main")),
],
targets: [
.target(
name: "SwiftUIExtensions",
dependencies: []
dependencies: [
"SwiftFoundationExtensions",
]
),
// NOTE: Re-enable when tests are added.
// .testTarget(

View File

@ -0,0 +1,28 @@
#if os(macOS)
import AppKit
import SwiftFoundationExtensions
import os
/**
Opens the given URL via the current shared workspace.
*/
public func sendUserToApplicationUrl(_ url: URL) {
if !NSWorkspace.shared.open(url) {
Logger.loggerFor("sendUserToApplicationUrl")
.warning("Unable to open URL: \(url, privacy: .public)")
}
}
/**
Opens the given URL string if it can be parsed and opened on the current device. Does nothing otherwise.
*/
public func sendUserToApplicationUrl(_ urlString: String) {
// Make sure the URL can be parsed first.
guard let url = URL(string: urlString) else {
return
}
sendUserToApplicationUrl(url)
}
#endif

View File

@ -0,0 +1,29 @@
#if os(iOS)
import UIKit
/**
Opens the given URL if it can be opened on the current device. Does nothing otherwise.
*/
public func sendUserToApplicationUrl(_ url: URL) {
// Ensure the url for the app can actually be opened.
guard UIApplication.shared.canOpenURL(url) else {
return
}
// Finally, send the user to the requested app url.
UIApplication.shared.open(url)
}
/**
Opens the given URL string if it can be parsed and opened on the current device. Does nothing otherwise.
*/
public func sendUserToApplicationUrl(_ urlString: String) {
// Make sure the URL can be parsed first.
guard let url = URL(string: urlString) else {
return
}
sendUserToApplicationUrl(url)
}
#endif

View File

@ -15,31 +15,6 @@ public func hideKeyboard() {
)
}
/**
Opens the given URL if it can be opened on the current device. Does nothing otherwise.
*/
public func sendUserToApplicationUrl(_ url: URL) {
// Ensure the url for the app can actually be opened.
guard UIApplication.shared.canOpenURL(url) else {
return
}
// Finally, send the user to the requested app url.
UIApplication.shared.open(url)
}
/**
Opens the given URL string if it can be parsed and opened on the current device. Does nothing otherwise.
*/
public func sendUserToApplicationUrl(_ urlString: String) {
// Make sure the URL can be parsed first.
guard let url = URL(string: urlString) else {
return
}
sendUserToApplicationUrl(url)
}
/**
Opens the App's setting screen in the OS' Settings app.
*/