在iOS 10.0中不推荐使用'openURL':请使用openURL:options:completionHandler:而不是在Swift 3中

Swi*_*per 33 uiwebview ios swift3

我已经开放了webLink网址代码,Swift3但是当我使用它时,会给我这个警告;

在iOS 10.0中不推荐使用'openURL':请使用openURL:options:completionHandler:而不是

我如何解决它,我的代码如下.

let myUrl = "http://www.google.com"
 if !myUrl.isEmpty {
                                UIApplication.shared.openURL(URL(string: "\(myUrl)")!)
                            }
Run Code Online (Sandbox Code Playgroud)

谢谢.

Anb*_*hik 86

用得像

 //inside scope use this
 let myUrl = "http://www.google.com"
    if let url = URL(string: "\(myUrl)"), !url.absoluteString.isEmpty {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }

    // or outside scope use this
    guard let url = URL(string: "\(myUrl)"), !url.absoluteString.isEmpty else {
       return
    }
     UIApplication.shared.open(url, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

有关更多参考,请参阅此示例链接.


Ser*_*gey 10

尝试使用此:

UIApplication.shared.open(URL(string: "\(myUrl)")!)
Run Code Online (Sandbox Code Playgroud)

  • 你读过这个问题吗? (4认同)
  • 这是一个正确、简洁的答案。接受的答案只是添加了一堆不相关的示例代码。 (4认同)
  • 更简单、更干净 (2认同)