我正在开发一个网页,它将由iOS和Android应用程序使用webview调用。
是否可以在网页内进行此操作,还是由打开webview的应用程序管理?怎么可能呢?
我读了一些关于UIWebViewDelegate但我不确定它是否是正确的解决方案。
谢谢
在http://domain.com/thanks.html#closeWebview等最终网址中使用哈希,然后观看网址。
在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);
}
}
});
您可以使用UIWebViewDelegate
方法来跟踪页面是否已加载并执行任何操作:
-(void)webViewDidFinishLoad:(UIWebView *)webView
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
-(void)webViewDidStartLoad:(UIWebView *)webView
例如:你可以使用didFinish:
-(void)webViewDidFinishLoad:(UIWebView *)webView{
if(finalURLReached)
[self dismissViewControllerAnimated:YES completion:nil];
}
使用新的WKWebView解决方案(不推荐使用UIWebView!):
if navigationAction.request.description.hasSuffix("/yourFinishUrlEnding") {
print("url matches...")
decisionHandler(.allow)
closeWebview()
} else {
decisionHandler(.allow)
}
}
将此添加到指定的WKNavigationDelegate:
func closeWebview() {
if(controller.presentingViewController != nil && (nil == controller.navigationController || controller.navigationController?.viewControllers.count < 2 )){
controller.dismiss(animated: true, completion: completion)
}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;
}
}
}