最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - iOS and Android webview: how to close webview and go back to App? - Stack Overflow

programmeradmin1浏览0评论

I am developing a webpage which will be called by an iOS and an Android App using a webview.

  1. This webpage would prompt a form to the user
  2. The user have to fill the fields and submit the form
  3. Once the form is submitted I redirect the user to another URL
  4. When this final URL is loaded, I need to close the webview

Is it possible to to this within the webpage or is it something to be managed by the app which open the webview? And how could it be possible?

I read something about UIWebViewDelegate but I'm not sure if it could be the right solution.

Thank you

I am developing a webpage which will be called by an iOS and an Android App using a webview.

  1. This webpage would prompt a form to the user
  2. The user have to fill the fields and submit the form
  3. Once the form is submitted I redirect the user to another URL
  4. When this final URL is loaded, I need to close the webview

Is it possible to to this within the webpage or is it something to be managed by the app which open the webview? And how could it be possible?

I read something about UIWebViewDelegate but I'm not sure if it could be the right solution.

Thank you

Share Improve this question asked Jan 13, 2016 at 8:53 webpaulwebpaul 1564 silver badges19 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Use a hash in your final URL like http://domain./thanks.html#closeWebview then watch URL.

On 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);
        }
    }
});

You can use UIWebViewDelegate methods to track if the page has been loaded and perform any operation:

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

For eg: you could use didFinish like:

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

Solution with new WKWebView (UIWebView is deprecated!):

  1. Listen to navigation events via WKNavigationDelegate

    webView.navigationDelegate = myNavigationDelegate // or use self when implementing it in your own WebView ViewController

  2. Listen to certain trigger URL in this delegate

    func webView(_ webView: WKWebView, decidePolicyFor 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. Close WebView (should works in all presentation variants)

Add this to your assigned WKNavigationDelegate:

func closeWebview() {
    if(controller.presentingViewController != nil && (nil == controller.navigationController || controller.navigationController?.viewControllers.count < 2 )){
        controller.dismiss(animated: true, pletion: pletion)
    }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;
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论