在我的ViewController的头文件中,我有:
@interface CaseStudyChoiceViewController : UIViewController <UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIWebView *myWebView;
@end
然后在ViewDidLoad方法的.m文件中:
[_myWebView setDelegate:self];
最后:
#pragma mark - WebView Delegate Methods
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:) ||
action == @selector(paste:) ||
action == @selector(select:) ||
action == @selector(cut:) ||
action == @selector(selectAll:))
{
NSLog(@"Selector is %@", NSStringFromSelector(action));
return NO;
}
else
{
return [super canPerformAction:action withSender:sender];
}
//I tried the code below as well and got the same result
/*
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController)
{
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
*/
}
我遇到的问题是不禁用复制功能。当我运行它时,NSLog输出是:
2013-12-30 21:39:28.794 PCO - CLS[45238:70b] Selector is cut:
2013-12-30 21:39:28.794 PCO - CLS[45238:70b] Selector is select:
2013-12-30 21:39:28.795 PCO - CLS[45238:70b] Selector is selectAll:
2013-12-30 21:39:28.795 PCO - CLS[45238:70b] Selector is paste:
copy:永远不会出现在 canPerformAction 方法中。关于我可以捕获它的任何想法?
最终结果是,当用户在UIWebView中进行长时间触摸时会弹出两个选项,复制和定义。我只想要定义。
答案 0 :(得分:1)
它适用于ios 7.0 - &gt; 9.2
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[webView stringByEvaluatingJavaScriptFromString:
@"document.documentElement.style.webkitUserSelect='none';"];
[webView stringByEvaluatingJavaScriptFromString:
@"document.documentElement.style.webkitTouchCallout='none';"];
}
答案 1 :(得分:0)
解决!
好的,所以这里的解决方案在这里描述:TPoschel的Disabling user selection in UIWebView与广告一样......直到你将.pdf文件加载到UIWebView中。加载.pdf会导致用户在UIWebView中长时间触摸后再次弹出复制/定义菜单。
经过另一次头发拉动之后,我在Johnny Rockex找到了这个答案,它就像一个冠军。 UIWebView without Copy/Paste when displaying PDF files
非常感谢他这个易于实施,天才的解决方案!事实上,这应该是一个通用的解决方案,并且无需使用webkit即可全面工作。