OSX Swift open URL in default browser

前端 未结 8 1309
情话喂你
情话喂你 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 15:01

    Just a bonus. If you want to open a URL in a specific browser(even other client who can handle that URL), here is the Swift 3 code tested on Xcode 8.2.1 and macOS 10.12.2.

    /// 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
      )
    }
    

提交回复
热议问题