【问题标题】:Detect tapped image inside UIWebView检测 UIWebView 中的点击图像
【发布时间】:2014-09-18 16:55:09
【问题描述】:

我有一个 web 视图,显示由其他人完成的 html + js 介绍。当用户点击特定图像时,我需要执行 segue。我不知道javascript。 如果我尝试使用 .src 返回空,如果尝试使用 .innerHTML 总是返回大量代码。 我怎样才能检测到这种特定的触摸?谢谢。

- (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];
    }

}

【问题讨论】:

    标签: javascript html ios uiwebview uiwebviewdelegate


    【解决方案1】:

    我认为你应该让你的图片在 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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 2015-08-31
      • 2011-10-17
      • 1970-01-01
      相关资源
      最近更新 更多