Jasper
Jasper

Reputation: 2404

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?

Upvotes: 1

Views: 4341

Answers (2)

Hardik Thakkar
Hardik Thakkar

Reputation: 15991

Here is a solution to disable long press context menu using UIKit Overrides for menu building and validation. Add this in your ViewController.m file

-(void)buildMenuWithBuilder:(id<UIMenuBuilder>)builder {
    NSLog(@"%@", builder);
    [builder removeMenuForIdentifier:UIMenuLookup];
    [builder removeMenuForIdentifier:UIMenuShare];
    [builder removeMenuForIdentifier:UIMenuStandardEdit];
}

Note: import UIKit #import <UIKit/UIKit.h>

Upvotes: 0

Jilouc
Jilouc

Reputation: 12714

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.

Upvotes: 4

Related Questions