[英]How to manipulate DOM in UIWebView in iOS
I am using a UIWebView in my app. 我在我的应用程序中使用UIWebView。 I want to call a objective-c function when user clicks a link in a website in UIWebView.
当用户在UIWebView中单击网站中的链接时,我想调用objective-c函数。 Is there a way to do this?
有没有办法做到这一点? Does UIWebView allow to change it?
UIWebView是否允许更改它?
First hook it. 首先勾住它。
[yourwebview setDelegate:self]
Use the following delegate method 使用以下委托方法
webView:shouldStartLoadWithRequest:navigationType
Like: 喜欢:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
//put your logic here, like
if (![url.scheme isEqual:@"yourscheme"]) {// or query contains or [url absolutestring] contains etc
return NO;
}
return YES;
}
If you want to manipulate DOM or call any function of UIWebView, then use: 如果你想操纵DOM或调用UIWebView的任何功能,那么使用:
[yourwebView stringByEvaluatingJavaScriptFromString:@"alert('hello')"];
[yourwebView stringByEvaluatingJavaScriptFromString:@"$('#domid').hide();"];
Just as stated in other answers, you should use webview delegate's 正如其他答案中所述,您应该使用webview委托
webView:shouldStartLoadWithRequest:navigationType
method to smuggle some data from within the WebView. 从WebView中走私某些数据的方法。 You can read my verbose answer on this topic here: how to use javascript in uiwebview to get the clicked button id?
你可以在这里阅读我对这个主题的详细解答: 如何在uiwebview中使用javascript来获取点击的按钮ID?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.