[CAKER-12] Adding open(URL) capability for macOS (#3)
Work on xiiagency/Caker#12
This commit is contained in:
parent
3a5f6bf664
commit
c08716eef2
|
@ -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(
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -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.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue