如何在移动 android/iOs(本机应用程序)中关闭 webview

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

在我们的应用程序中,在 webview 中打开一个 url 有一些特定的 url 检测后关闭 webview 的方法。 怎么可能关闭 webview ?我已经尝试在 javascript.but 中使用 window.close() 无法 work.have 从 android 或 ios app.

的另一种方式

您可以使用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.

备注:

  • 使用 POST "method".
  • 的请求不会调用此方法
  • 对于非 http 方案的子帧也会调用此方法,因此强烈建议从方法内部使用请求的 url 无条件调用 loadUrl(String) 然后 return是的,因为这会使 WebView 尝试加载非 http url,从而失败。

这是示例演示

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://whosebug.com/users/7666442/nilesh-rathod?tab=topactivity");

    }


    public class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.equals("https://whosebug.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;
            }
        }
    }


}

根据对问题的评论,我为关闭网络视图设置了一个更好的术语 - 返回上一个屏幕。您可以对 Android 和 iOS 执行以下操作:

Android :

finish()  

iOS :

如果您使用的是导航控制器:

self.navigationController?.popViewController(animated: true)  

如果您正在展示 Web 视图控制器:

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

关键是在web视图的delegate函数中检查url。

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
    }

使用这个可以帮助你

  myWebView.destroy();
  myWebView = null;

在 iOS 中,您现在将使用 Safari sfview,因此出于安全考虑,直接获取 URL 被阻止,因此您可以在 Android 和 [= 中应用的唯一通用解决方案17=] 应用很深 linking.

当您使用深度 link 到达特定 URL 时,您可以触发关闭 safari sf 视图。

Google 深入 link 并按照示例进行操作。