How to close webview in mobile android/iOs(Native app)
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
. IfWebViewClient
is not provided, by defaultWebView
will ask Activity Manager to choose the proper handler for the url. IfWebViewClient
is provided, return true means the host application handles the url, while return false means the currentWebView
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 makeWebView
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;
}
}
}
}
Bhavesh Chauhan
Updated on June 04, 2022Comments
-
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 about 6 yearsYou can simply set visibility of that webview.
-
Nitish about 6 yearsNative or hybrid ? Which technology ?
-
Upendra Shah about 6 yearsNative android :: wbview.removeAllViews(); wbview.destroy();
-
Bhavesh Chauhan about 6 yearsIn native app have an issue
-
Nitish about 6 yearsBy closing web view, do you mean navigating back from web view screen to the previous screen ?
-
Bhavesh Chauhan about 6 yearsyes .navigating back from web view screen to the previous screen
-
Venk about 6 years@BhaveshChauhan show me your native code.
-
Abi about 6 yearsYou can set visibility of that webview as said by @UpendraShah
-
Vladyslav Matviienko about 6 yearsyou can't close WebVIew, it is not something that can be closed. What do you mean by closing a WebView?
-
Umar Hussain about 6 yearsprovide the code how you are doing this, which is creating issue.
-
-
Nitish about 6 yearsOP has already mentioned in the question comments that he wants to go to previous screen.
-
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 about 6 years@nilesh here need to create custom webview?(as per my android developer say)
-
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