Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am currently implementing a WKWebView into my application and I want to use the decidePolicyFor delegation method to decide how my app responds to external links.

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
    if navigationAction.navigationType == WKNavigationType.linkActivated && !(navigationAction.request.url?.host?.lowercased().hasPrefix("www.example.com"))! {
        UIApplication.shared.open(navigationAction.request.url!, options: [:], completionHandler:nil)
        decisionHandler(.allow)
        decisionHandler(.cancel)

However the compiler is giving me the following issue in swift 3.0 :-

Instance method 'webView(:decidePolicyFor:decisionHandler:)' nearly matches optional requirement 'webView(:decidePolicyFor:decisionHandler:)' of protocol 'WKNavigationDelegate'

Does anybody know the solution to this and more importantly why this is happening

Thanks in advance

When you find something odd in Xcode, please check the latest reference. (Some of the references may not be up-to-date, but luckily, the description of the delegate method seems to be up-to-date.)

Declaration

optional func webView(_ webView: WKWebView, 
      decidePolicyFor navigationAction: WKNavigationAction, 
      decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)

Add @escaping after decisionHandler:.

func webView(_ webView: WKWebView,
             decidePolicyFor navigationAction: WKNavigationAction,
             decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.