Skip to content

Instantly share code, notes, and snippets.

@lucas34
Last active March 2, 2018 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucas34/a9e5a07178a508bc2d5c7009b3c72693 to your computer and use it in GitHub Desktop.
Save lucas34/a9e5a07178a508bc2d5c7009b3c72693 to your computer and use it in GitHub Desktop.
Open url on any Apple os
import Foundation
#if os(macOS)
import Cocoa
#endif
#if os(iOS) || os(tvOS)
extension URL {
public func canOpen() -> Bool {
return UIApplication.shared.openURL(self)
}
public func open() -> Bool {
if canOpen() {
UIApplication.shared.openURL(self)
return true
}
return false
}
}
#elseif os(macOS)
extension URL {
public func canOpen() -> Bool {
return true
}
public func open() -> Bool {
NSWorkspace.shared().open(self)
return true
}
}
#else
extension URL {
public func canOpen() -> Bool {
return false
}
public func open() -> Bool {
return false
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment