2014-11-02 7 views
49

So öffnen Sie eine URL im Systemstandardbrowser, indem Sie Swift als Programmiersprache und OSX als Plattform verwenden.OSX Swift URL im Standardbrowser öffnen

fand ich viel mit UIApplication wie

UIApplication.sharedApplication().openURL(NSURL(string: object.url)) 

aber das funktioniert nur auf iOS und nicht auf OSX

Und die Launch Services, fand ich keine Beispiele für eine rasche hat und es gibt eine Menge veraltet für OSX 10.10

Jede Hilfe willkommen - danke.

+0

Ich denke, es ist, weil wir stattdessen die neuen Erweiterungen zu verwenden sind angeblich ... –

+1

ich upvoted, weil ich weiß, wie neede, dass nur in iOS zu tun xd – Josh

Antwort

86

Xcode 8.1 • Swift 3.0.1

import Cocoa 

if let url = URL(string: "https://www.google.com"), NSWorkspace.shared().open(url) { 
    print("default browser was successfully opened") 
} 
25

Für iOS, können Sie die folgende verwenden:

let url = NSURL(string: "https://google.com")! 
UIApplication.sharedApplication().openURL(url) 

Sie haben NSURL auszupacken.

+18

Die Frage ist hier OS X, nicht iOS !!! –

+7

Warum hat das 23 upvotes? Off topic auf die Frage und sollte entfernt werden. –

+0

funktioniert es auch für iOS. Ich habe gestimmt. – Lin

7
NSWorkspace.sharedWorkspace().openURL(NSURL(string: "https://google.com")!) 

ODER

UIApplication.sharedApplication().openURL(NSURL(string: "https://google.com")!) 
+4

Zweite Antwort ist nicht für OSX ... –

+0

Gute Referenz für frühere Versionen von Swift (Ich bin auf 2.3) –

+0

Bitte beschreibe, dass der zweite Codeblock für iOS ist –

8

Wenn mit Swift 3, können Sie eine Webseite im Standardbrowser öffnen die mit folgenden:

NSWorkspace.shared().open(NSURL(string: "https://google.com")! as URL) 

In der akzeptierte Antwort oben, können Sie Überprüfen Sie auch eine URL mit Swift 3, indem Sie Folgendes eingeben:

if let checkURL = NSURL(string: "https://google.com") { 
    if NSWorkspace.shared().open(checkURL as URL) { 
     print("URL Successfully Opened") 
    } 
} else { 
    print("Invalid URL") 
} 

Ich hoffe, dass diese Informationen helfen, wen auch immer es gilt.

+0

Da dies Swift 3 ist, glaube ich nicht, dass NSURL mehr notwendig ist - benutze einfach URL: NSWorkspace.shared(). Open (URL (string: " https://google.com ")) und wenn checkURL = URL (string lassen: "https://google.com") { wenn NSWorkspace.shared() geöffnet (checkURL) { Druck. ("URL erfolgreich geöffnet") } } else { Drucken ("Ungültige URL") } – gepree

6

Nur ein Bonus. Wenn Sie eine URL in einem bestimmten Browser öffnen möchten (auch andere Clients, die mit dieser URL umgehen können), ist hier der Code Swift 3 auf Xcode 8.2.1 und macOS 10.12.2 getestet.

/// appId: `nil` use the default HTTP client, or set what you want, e.g. Safari `com.apple.Safari` 
func open(url: URL, appId: String? = nil) -> Bool { 
    return NSWorkspace.shared().open(
    [url], 
    withAppBundleIdentifier: appId, 
    options: NSWorkspaceLaunchOptions.default, 
    additionalEventParamDescriptor: nil, 
    launchIdentifiers: nil 
) 
} 
3

xCode 9 Update

let url = URL(string: "https://www.google.com")! 

UIApplication.shared.open(url, options: [:], completionHandler: nil)