大家常常在开发中会碰到这样的需求–禁用网页或PDF文件中放大镜及拷贝粘贴弹出框
查询了很多资料发现如下方法已经失效
webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitUserSelect='none';")
webView.stringByEvaluatingJavaScriptFromString("document.documentElement.style.webkitTouchCallout='none';")
你可以试一下如下方法
由于网页长按手势默认时间是0.5秒,所有我们在设置长按手势时间要小于0.5秒就行
-(void)createUI{
_myWebView = [[UIWebView alloc]initWithFrame:self.view.bounds];
NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"];
[_myWebView loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:_myWebView];
// 加载pdf文件方法
// NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"];
//
// NSURLRequest* pdfRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
// [_myWebView loadRequest:pdfRequest];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil];
longPress.delegate = self; //记得在.h文件里加上<UIGestureRecognizerDelegate>委托
longPress.minimumPressDuration = 0.4; //这里为什么要设置0.4,因为只要大于0.5就无效,我想是因为默认的跳出放大镜的手势的长按时间是0.5秒,
[_myWebView addGestureRecognizer:longPress];
}
#pragma mark - GestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return NO; //这里一定要return NO,至于为什么大家去看看这个方法的文档吧。
}