我的可可应用程序(macosx 不是 iphone)中有一个 webView,它向用户显示一些 javascript,但我不希望用户能够选择任何文本或右键单击并选择重新加载选项。
有没有一种简单的方法可以禁用与 webView 的所有交互?
我知道在 iPhone 上禁用用户交互很容易,但我以前从未需要在桌面上执行此操作并且找不到解决方案。
设置编辑和 UI 委托:
[view setUIDelegate:self];
[view setEditingDelegate:self];
然后添加上面的方法来禁用文本选择和上下文菜单。
- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element
defaultMenuItems:(NSArray *)defaultMenuItems
{
// disable right-click context menu
return nil;
}
- (BOOL)webView:(WebView *)webView shouldChangeSelectedDOMRange:(DOMRange *)currentRange
toDOMRange:(DOMRange *)proposedRange
affinity:(NSSelectionAffinity)selectionAffinity
stillSelecting:(BOOL)flag
{
// disable text selection
return NO;
}
通过 UI 委托,禁用拖放操作也可能很有用,添加
- (NSUInteger)webView:(WebView *)sender dragSourceActionMaskForPoint:(NSPoint)point
{
return WebDragSourceActionNone; // Disable any WebView content drag
}
- (NSUInteger)webView:(WebView *)sender dragDestinationActionMaskForDraggingInfo:(id <NSDraggingInfo>)draggingInfo
{
return WebDragDestinationActionNone; // Disable any WebView content drop
}