Swift Open Link in Safari

前端 未结 10 2421
北荒
北荒 2020-12-01 00:41

I am currently opening the link in my app in a WebView, but I\'m looking for an option to open the link in Safari instead.

相关标签:
10条回答
  • 2020-12-01 01:02

    Swift 3 & IOS 10.2

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

    Swift 3 & IOS 10.2

    0 讨论(0)
  • 2020-12-01 01:03

    In Swift 2.0:

    UIApplication.sharedApplication().openURL(NSURL(string: "http://stackoverflow.com")!)
    
    0 讨论(0)
  • 2020-12-01 01:03

    Swift 5

    if let url = URL(string: "https://www.google.com") {
        UIApplication.shared.open(url)
    }
    
    0 讨论(0)
  • 2020-12-01 01:17

    It's not "baked in to Swift", but you can use standard UIKit methods to do it. Take a look at UIApplication's openUrl(_:) (deprecated) and open(_:options:completionHandler:).

    Swift 4 + Swift 5 (iOS 10 and above)

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

    Swift 3 (iOS 9 and below)

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

    Swift 2.2

    guard let url = URL(string: "https://stackoverflow.com") else { return }
    UIApplication.sharedApplication().openURL(url)    
    
    0 讨论(0)
提交回复
热议问题