GTeckHan Goh
GTeckHan Goh

Reputation: 97

openURL and open in IOS 9 / 10

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

Can I use these two options in iOS9 and iOS10?

Is UIApplication.shared.openURL(url) supported in iOS9 and/or iOS10?

Upvotes: 3

Views: 6056

Answers (3)

Hitesh
Hitesh

Reputation: 896

You can try this

guard let url = URL(string: "http://www.google.com") else {
  return //be safe
}

if #available(iOS 10.0, *) {
    UIApplication.shared.open(url)
} else {
    UIApplication.shared.openURL(url)
}

Upvotes: 8

Pankaj Negi
Pankaj Negi

Reputation: 1618

guard let url = URL(string: "https://www.google.com/") else { return }
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url)
        } else {
            // Fallback on earlier versions
            let svc = SFSafariViewController(url: url)
            present(svc, animated: true, completion: nil)
        }

I used this when I want to Open a URL in Safari browser in iOS 9 and above.

Upvotes: 0

dahiya_boy
dahiya_boy

Reputation: 9503

Yes you have to use both by putting them in condition like this,

func open(scheme: String) {
  if let url = URL(string: scheme) {
    if #available(iOS 10, *) { // For ios 10 and greater
      UIApplication.shared.open(url, options: [:],
        completionHandler: {
          (success) in
           print("Open \(scheme): \(success)")
       })
    } else { // for below ios 10.
      let success = UIApplication.shared.openURL(url)
      print("Open \(scheme): \(success)")
    }
  }
}

Upvotes: 3

Related Questions