【问题标题】:iOS and Android webview: how to close webview and go back to App?iOS和Android webview:如何关闭webview并返回App?
【发布时间】:2016-04-18 03:48:06
【问题描述】:

我正在开发一个网页,该网页将被 iOSAndroid 应用程序调用,该应用程序使用 webview

  1. 此网页会向用户提示表单
  2. 用户必须填写字段并提交表单
  3. 提交表单后,我将用户重定向到另一个 URL
  4. 加载最终 URL 后,我需要关闭 web 视图

是否可以在网页中执行此操作,还是由打开 webview 的应用程序管理?又怎么可能呢?

我阅读了一些关于 UIWebViewDelegate 的内容,但我不确定它是否是正确的解决方案。

谢谢

【问题讨论】:

    标签: javascript android ios webview uiwebviewdelegate


    【解决方案1】:

    在您的最终到达网址中使用哈希,例如 http://domain.com/thanks.html#closeWebview,然后观看网址。

    在安卓上:

    mWebview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (url.endsWith("#closeWebview")){
                mWebview.setVisibility(View.GONE);
            }
        }
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用UIWebViewDelegate方法来跟踪页面是否已加载并执行任何操作:

      -(void)webViewDidFinishLoad:(UIWebView *)webView
      -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
      -(void)webViewDidStartLoad:(UIWebView *)webView
      

      例如:您可以使用 didFinish 之类的:

      -(void)webViewDidFinishLoad:(UIWebView *)webView{
           if(finalURLReached)
               [self dismissViewControllerAnimated:YES completion:nil];
      }
      

      【讨论】:

        【解决方案3】:

        使用新的 WKWebView 的解决方案(UIWebView 已弃用!):

        1. 通过WKNavigationDelegate

          收听导航事件

          webView.navigationDelegate = myNavigationDelegate // 或者在自己的WebView ViewController中实现时使用self

        2. 收听此委托中的某些触发 URL

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

          print("请求:(navigationAction.request.description)")

              if navigationAction.request.description.hasSuffix("/yourFinishUrlEnding") {            
                  print("url matches...")
                  decisionHandler(.allow)
                  closeWebview()
              } else {
                  decisionHandler(.allow)
              }
          }
          
        3. 关闭 WebView(应该适用于所有演示变体)

        将此添加到您分配的 WKNavigationDelegate:

        func closeWebview() {
            if(controller.presentingViewController != nil && (nil == controller.navigationController || controller.navigationController?.viewControllers.count < 2 )){
                controller.dismiss(animated: true, completion: completion)
            }else{
                controller.navigationController?.delegate = nil;
                // self.animationInteractor.forceCompleteAnimation(.Left)
                let c = controller.navigationController?.viewControllers.count
                let ct = c! - 2;
                controller.navigationController?.popViewController(animated: true)
                if controller.navigationController?.viewControllers[ct] is UITransitionViewController {
                    controller.navigationController?.transitioningDelegate = controller.navigationController?.viewControllers[ct] as! UITransitionViewController;
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-03
          • 2013-10-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多