How to close webview in mobile android/iOs(Native app)

11,854

Solution 1

As per comments on question, I am putting a better term for closing the web view - going back to previous screen. You can do this as follows for Android and iOS :

Android :

finish()  

iOS :

If you are using navigation controller :

self.navigationController?.popViewController(animated: true)  

If you are presenting web view controller :

self.dismiss(animated: true, completion: nil)  

The key is to check that url in the delegate function of web view.

Android :

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.equals("your_url")) {
            finish()
            return false;
        } else {
            return true;
        }
    }  

iOS :

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if request.url?.absoluteString == "your_url" {
            self.navigationController?.popViewController(animated: true)
            // If controller is presented -  self.dismiss(animated: true, completion: nil)  
            return false
        }

        return true
    }

Solution 2

You can use shouldOverrideUrlLoading

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url.

Notes:

  • This method is not called for requests using the POST "method".
  • This method is also called for subframes with non-http schemes, thus it is strongly disadvised to unconditionally call loadUrl(String) with the request's url from inside the method and then return true, as this will make WebView to attempt loading a non-http url, and thus fail.

Here is the sample demo

public class MainActivity extends AppCompatActivity {

    WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView = findViewById(R.id.myWebView);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl("https://stackoverflow.com/users/7666442/nilesh-rathod?tab=topactivity");

    }


    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.equals("https://stackoverflow.com/users/7666442/nilesh-rathod?tab=profile")) {

                finish() ;
                Toast.makeText(MainActivity.this, "URL DETECTED", Toast.LENGTH_SHORT).show();
                // perform your action here
                return true;
            } else {
                view.loadUrl(url);
                return true;
            }
        }
    }


}
Share:
11,854
Bhavesh Chauhan
Author by

Bhavesh Chauhan

Updated on June 04, 2022

Comments

  • Bhavesh Chauhan
    Bhavesh Chauhan almost 2 years

    In our app have opening one url in webview there is way to close webview after some specific url detect. how can possible to close webview ? I have try with window.close() in javascript.but could not work.have another way from android or ios app.

    • Upendra Shah
      Upendra Shah about 6 years
      You can simply set visibility of that webview.
    • Nitish
      Nitish about 6 years
      Native or hybrid ? Which technology ?
    • Upendra Shah
      Upendra Shah about 6 years
      Native android :: wbview.removeAllViews(); wbview.destroy();
    • Bhavesh Chauhan
      Bhavesh Chauhan about 6 years
      In native app have an issue
    • Nitish
      Nitish about 6 years
      By closing web view, do you mean navigating back from web view screen to the previous screen ?
    • Bhavesh Chauhan
      Bhavesh Chauhan about 6 years
      yes .navigating back from web view screen to the previous screen
    • Venk
      Venk about 6 years
      @BhaveshChauhan show me your native code.
    • Abi
      Abi about 6 years
      You can set visibility of that webview as said by @UpendraShah
    • Vladyslav Matviienko
      Vladyslav Matviienko about 6 years
      you can't close WebVIew, it is not something that can be closed. What do you mean by closing a WebView?
    • Umar Hussain
      Umar Hussain about 6 years
      provide the code how you are doing this, which is creating issue.
  • Nitish
    Nitish about 6 years
    OP has already mentioned in the question comments that he wants to go to previous screen.
  • AskNilesh
    AskNilesh about 6 years
    @Nitish thanks i have updated my BTW i have also added comment to perform his reqired action as per requirement in my answer please check again
  • Bhavesh Chauhan
    Bhavesh Chauhan about 6 years
    @nilesh here need to create custom webview?(as per my android developer say)
  • AskNilesh
    AskNilesh about 6 years
    @BhaveshChauhan no need to create custom webview just create new custom WebViewClient same as in my answer it will work fine