2014-09-18 55 views
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]; 
    } 

} 

回答

0

我认为你应该让你的图像在html可点击。如果是这样,就忽略这一点。 然后你应该使用这样的UIWebView的shouldStartLoadWithRequest委托方法:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ 


    if (navigationType == UIWebViewNavigationTypeLinkClicked){ 

     NSURL *url = request.URL; 
     //Maybe you can detect the image here looking to the url. 

    } 

    return YES; 

}