iOS和Android webview:如何关闭webview并返回App?

问题描述 投票:3回答:3

我正在开发一个网页,它将由iOS和Android应用程序使用webview调用。

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

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

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

谢谢

javascript android ios webview uiwebviewdelegate
3个回答
4
投票

http://domain.com/thanks.html#closeWebview等最终网址中使用哈希,然后观看网址。

在Android上:

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];
}

0
投票

使用新的WKWebView解决方案(不推荐使用UIWebView!):

  1. 通过WKNavigationDelegate收听导航事件 webView.navigationDelegate = myNavigationDelegate //或在您自己的WebView ViewController中实现它时使用self
  2. 收听此委托中的某个触发器URL func webView(_ webView:WKWebView,decisionPolicyFor navigationAction:WKNavigationAction,decisionHandler:@escaping(WKNavigationActionPolicy) - > Void){ print(“request:(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;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.