Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS and Android webview: how to close webview and go back to App?

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

like image 886
webpaul Avatar asked Oct 27 '25 15:10

webpaul


2 Answers

Use a hash in your final URL like http://domain.com/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);
        }
    }
});
like image 199
JeanK Avatar answered Oct 29 '25 05:10

JeanK


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 completion:nil];
}
like image 30
Atif Imran Avatar answered Oct 29 '25 05:10

Atif Imran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!