Search code examples
iphoneobjective-cioswebview

How can disable WebView's contextmenu?


I want to disable WebView's contextmenu "Reload", "Open Link", "Open Link in new Window", "Download link" etc.. I've tried a really long time, this method with contextMenuItemsForElement, but no matter how I try do not work I really feel very sad, I hope someone can help me, I would be very grateful.

The following is my code:

@class WebView;
@interface UIWebView (Client)
- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element defaultMenuItems:(NSArray *)defaultMenuItem;
@end

@implementation UIWebView (Client)
- (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element 
    defaultMenuItems:(NSArray *)defaultMenuItems
{
    NSLog(@"contextMenuItemsForElement");
    return nil;
}

@end

Why are not working?


Solution

  • I don't think webView:contextMenuItemsForElements: is available in the iPhone SDK (or at least publicly).

    If you have control over the html/css code, you can put this rule in your css

    a {
        -webkit-touch-callout: none !important;
    }
    

    and if it isn't possible, try

    [yourWebView stringByEvaluatingJavascriptFromString:@"var a = document.getElementsByTagName('a'); for (var i = 0; i < a.length; i++) { a.style.WebkitTouchCallout = 'none'; }"];
    

    in your webViewDidFinishLoad: method.