0
我有一个web视图,显示其他人完成的html + js介绍。当用户点击特定图像时,我需要执行一个搜索。我不知道JavaScript。 如果我尝试使用.src返回空值,如果使用.innerHTML尝试返回大量代码。 如何检测这种特定的触摸?谢谢。检测UIWebView内的挖掘图像
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
[tap setDelegate:self];
[self.webView.scrollView addGestureRecognizer:tap];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"html" inDirectory:@"myDir"];
NSURL* url = [[NSURL alloc]initFileURLWithPath:htmlFile];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(void)viewTapped:(UITapGestureRecognizer*)tap {
CGPoint touchPoint = [tap locationInView:self.webView];
NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];
NSString *urlToSave = [self.webView stringByEvaluatingJavaScriptFromString:js];
//Returning an empty string
if (urlToSave.length > 0) {
[self performSegueWithIdentifier:@"introToMain" sender:self];
}
}