How to open a url in safari even if my default browser is not Safari using Swift in macOS?
You can use NSWorkspace open method which has been deprecated in macOS 11
let url = URL(string: "https://www.google.com")!
NSWorkspace.shared.open([url], withAppBundleIdentifier: "com.apple.safari", options: .default, additionalEventParamDescriptor: nil, launchIdentifiers: nil)
Or the replacement method (macOS 10.15 or later)
do {
let safariURL = try FileManager.default.url(for: .applicationDirectory, in: .localDomainMask, appropriateFor: nil, create: false).appendingPathComponent("Safari.app")
NSWorkspace.shared.open([url], withApplicationAt: safariURL, configuration: .init()) { (runningApp, error) in
print("running app", runningApp ?? "nil")
}
} catch {
print(error)
}
OSX Swift open URL in default browser
Swift 3 or later
import Cocoa
let url = URL(string: "https://www.google.com")!
if NSWorkspace.shared.open(url) {
print("default browser was successfully opened")
}
Swift Open Link in Safari
It's not "baked in to Swift", but you can use standard UIKit
methods to do it. Take a look at UIApplication's openUrl(_:)
(deprecated) and open(_:options:completionHandler:)
.
Swift 4 + Swift 5 (iOS 10 and above)
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.open(url)
Swift 3 (iOS 9 and below)
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.openURL(url)
Swift 2.2
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.sharedApplication().openURL(url)
WKWebView open links from certain domain in safari
You can implement WKNavigationDelegate
, add the decidePolicyForNavigationAction
method and check there the navigationType and requested url. I have used google.com below but you can just change it to your domain:
Xcode 8.3 • Swift 3.1 or later
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
let webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
webView.frame = view.bounds
webView.navigationDelegate = self
let url = URL(string: "https://www.google.com")!
let urlRequest = URLRequest(url: url)
webView.load(urlRequest)
webView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
view.addSubview(webView)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
if let url = navigationAction.request.url,
let host = url.host, !host.hasPrefix("www.google.com"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
print(url)
print("Redirected to browser. No need to open it locally")
decisionHandler(.cancel)
return
} else {
print("Open it locally")
decisionHandler(.allow)
return
}
} else {
print("not a user click")
decisionHandler(.allow)
return
}
}
}
Related Topics
Take Screenshot of Host App Using iOS Share/Action Extensions
Querying in Firebase by Child of Child
Pattern Matching in a Swift for Loop
Disable Bounce Scrolling for Wkwebview in MACos
Get the String Up to a Specific Character
[_Nscftimer Copywithzone:]: Unrecognized Selector Sent to Instance
Swift: How to Create a Predicate with an Int Value
How to Add Documentation to Enum Associated Values in Swift
No Designated Init for Skshapenode(Circleofradius: Radius)
Table View Cellforrowatindexpath Warning
"Message from Debugger: Unable to Attach" When Running Tests on Osx App
What's the Rationale of Swift's Size Methods Taking 'Int'S
Need Explanation About Random Function Swift
How to Prompt for Accessibility Features in a MACos App (From the Appdelegate)