Cocoa webView - Disable all interaction

前端 未结 2 1387
既然无缘
既然无缘 2020-12-05 15:20

I have a webView in my cocoa application (macosx not iphone), it displays some javascript to the user but I don\'t want the user to be able to select any of the text or righ

相关标签:
2条回答
  • 2020-12-05 16:07

    Set both editing and UI delegations:

    [view setUIDelegate:self];
    [view setEditingDelegate:self];
    

    Then add the methods above to disable text selection and context menu.

    - (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;
    }
    
    0 讨论(0)
  • 2020-12-05 16:25

    via the UI delegation it might be useful also to disable drag and drop operations, add

    - (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
    }
    
    0 讨论(0)
提交回复
热议问题