仅调用第一个请求UIWebView

时间:2016-11-25 09:13:48

标签: ios uiwebview swift3 deep-linking

我有一个网址请求让我们说

        let url = URL(string: "https://www.google.com/share?referCode=RSJDofpeW")

我们想在WebView中打开此URL,此链接也与通用链接相关联。因此,打开此URL将打开已安装的应用程序,但我想在UIWebView中加载页面。所以我检查了代表,发现它第一次调用它是上面的url然后下次它将添加scheme和appstore重定向。

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        log.verbose(webView)
        log.verbose(request)
        log.error(navigationType.rawValue)
        if request.url?.scheme == "itms-appss" || request.url?.scheme == "googleApp"{
            return false
        }

        return true
    }

因此,为了克服在Web View中未加载页面的问题,我执行了上述代码,因此当scheme为itms-appssgoogleApp时,它将不会加载请求但是第一次加载请求时是正确的网址,它应该加载该页面,但没有打开。

1 个答案:

答案 0 :(得分:1)

//检查

 var isUrlLoaded : Bool = false;

//委托函数

  func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
//check is the url is loaded for first time or not           
 if isUrlLoaded != true{
                if request.url?.scheme == "itms-appss" || request.url?.scheme == "googleApp"{
                    return true
                }
            }

            return false
        }
        func webViewDidFinishLoad(_ webView: UIWebView) {
            if  webView.request?.url?.scheme == "itms-appss" || webView.request?.url?.scheme == "googleApp"{
         // is url loaded       
         isUrlLoaded = true;
            }
        }