OSX Swift open URL in default browser

前端 未结 8 1149
情话喂你
情话喂你 2020-12-04 14:10

How to open a url in system default browser by using Swift as programming language and OSX as plattform.

I found a lot with UIApplication like

UIAppl         


        
8条回答
  •  有刺的猬
    2020-12-04 14:53

    When using Swift 3, you can open a webpage in the default browser using the following:

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

    In the accepted answer above, you can also check a URL using Swift 3 by inputting the following:

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

    I hope that this information helps whomever it applies to.

提交回复
热议问题