Files
NetNewsWire/Frameworks/RSWeb/RSWeb/MacWebBrowser.swift

45 lines
1016 B
Swift
Executable File

//
// MacWebBrowser.swift
// RSWeb
//
// Created by Brent Simmons on 12/27/16.
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
//
import AppKit
public class MacWebBrowser {
@discardableResult public class func openURL(_ url: URL, inBackground: Bool) -> Bool {
guard let preparedURL = url.preparedForOpeningInBrowser() else {
return false
}
if (inBackground) {
do {
try NSWorkspace.shared.open(preparedURL, options: [.withoutActivation], configuration: [:])
return true
}
catch {
return false
}
}
return NSWorkspace.shared.open(preparedURL)
}
}
private extension URL {
func preparedForOpeningInBrowser() -> URL? {
var urlString = absoluteString.replacingOccurrences(of: " ", with: "%20")
urlString = urlString.replacingOccurrences(of: "^", with: "%5E")
urlString = urlString.replacingOccurrences(of: "&", with: "&")
urlString = urlString.replacingOccurrences(of: "&", with: "&")
return URL(string: urlString)
}
}