UFO ET IT

기본 브라우저에서 OSX Swift 열기 URL

ufoet 2020. 11. 12. 20:41
반응형

기본 브라우저에서 OSX Swift 열기 URL


Swift를 프로그래밍 언어로 사용하고 OSX를 plattform으로 사용하여 시스템 기본 브라우저에서 URL을 여는 방법.

UIApplication으로 많은 것을 발견했습니다.

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

하지만 이것은 OSX가 아닌 iOS에서만 작동합니다.

그리고 Launch Services 에는 swift에 대한 예제가 없으며 OSX 10.10에서 더 이상 사용되지 않는 항목이 많이 있습니다.

어떤 도움이라도 환영합니다-감사합니다.


Swift 3 이상

import Cocoa

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

}

MacOS의 경우 다음을 사용할 수 있습니다.

let url = URL(string: "https://www.stackoverflow.com")!
NSWorkspace.sharedWorkspace().openURL(url))

iOS의 경우 다음을 사용할 수 있습니다.

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

NSURL을 풀어야합니다.


맥 OS:

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

iOS :

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

사용하는 경우 스위프트 3 , 당신은 다음을 사용하여 기본 브라우저에서 웹 페이지를 열 수 있습니다 :

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

위의 답변 에서 다음을 입력 하여 Swift 3사용하여 URL을 확인할 수도 있습니다 .

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

이 정보가 적용되는 모든 사람에게 도움이되기를 바랍니다.


xCode 9 업데이트

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

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

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

For Swift 5, Xcode 10 and MAC OS:

NSWorkspace.shared.open(NSURL(string: "http://www.lichess.org")! as URL)

MacOS Xcode 10 Swift 4.2 update

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

참고URL : https://stackoverflow.com/questions/26704852/osx-swift-open-url-in-default-browser

반응형

'UFO ET IT' 카테고리의 다른 글

하다  (0) 2020.11.12
페이지 수를 계산하는 가장 간단한 공식은?  (0) 2020.11.12
document.body.scrollTop Firefox에서 0 반환 : JS 만  (0) 2020.11.12
npm으로 bcrypt 설치 오류  (0) 2020.11.12
FactoryGirl 및 다형성 연관  (0) 2020.11.12