【发布时间】: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