[webview] Safari에서 Swift Open Link

현재 내 앱에서 링크를 여는 WebView중이지만 Safari 에서 링크를 여는 옵션을 찾고 있습니다.



답변

“Swift에 구워진”것은 아니지만 표준 UIKit방법을 사용 하여 수행 할 수 있습니다. UIApplication (더 이상 사용되지 않음) 및을 살펴보십시오 .openUrl(_:) open(_:options:completionHandler:)

스위프트 4 + 스위프트 5 (iOS 10 이상)

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.open(url)

스위프트 3 (iOS 9 이하)

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.openURL(url)

스위프트 2.2

guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.sharedApplication().openURL(url)    


답변

iOS 9 이상의 새로운 기능으로 사용자에게 SFSafariViewController(문서를 참조 하십시오 ) 제시 할 수 있습니다 . 기본적으로 앱을 떠나지 않고도 사용자를 Safari로 보내는 모든 이점을 얻을 수 있습니다. 새로운 SFSafariViewController를 사용하려면 :

import SafariServices

이벤트 핸들러 어딘가에 사용자에게 다음과 같이 사파리 뷰 컨트롤러를 제공하십시오.

let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)

사파리보기는 다음과 같습니다.

여기에 이미지 설명을 입력하십시오


답변

스위프트 4 업데이트 : (마르코 베버에게 신용)

if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
     UIApplication.shared.openURL(requestUrl as URL)
}

또는 다음을 사용하여 더 빠른 스타일로 이동하십시오 guard.

guard let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") else {
    return
}

UIApplication.shared.openURL(requestUrl as URL) 

스위프트 3 :

다음을 통해 NSURL을 선택적으로 암시 적으로 확인할 수 있습니다.

if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
     UIApplication.sharedApplication().openURL(requestUrl)
}


답변

스위프트 3 & IOS 10.2

UIApplication.shared.open(URL(string: "http://www.stackoverflow.com")!, options: [:], completionHandler: nil)

스위프트 3 & IOS 10.2


답변

iOS 10부터 다음을 사용해야합니다.

guard let url = URL(string: linkUrlString) else {
    return
}

if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
    UIApplication.shared.openURL(url)
}


답변

스위프트 5

스위프트 5 : canOpneURL유효한지 확인한 다음 열려 있는지 확인하십시오 .

guard let url = URL(string: "https://iosdevcenters.blogspot.com/") else {
     return
}

if UIApplication.shared.canOpenURL(url) {
     UIApplication.shared.open(url, options: [:], completionHandler: nil)
}


답변

스위프트 1.2에서 :

@IBAction func openLink {
    let pth = "http://www.google.com"
    if let url = NSURL(string: pth){
        UIApplication.sharedApplication().openURL(url)
}