Dheeraj Chahar Dheeraj Chahar - 1 year ago 168
iOS Question

How to disable copy share option while selecting text in uiwebview? But selecting text should work

I want to restrict user from copying and sharing text in


The code i'm using is

- (void)viewDidLoad {
[super viewDidLoad];
self.webview.delegate = self;
NSString *path = [[NSBundle mainBundle] pathForResource:@"SampleHTML" ofType:@"html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.webview loadHTMLString:htmlContent baseURL:[NSURL URLWithString:@""]];

// Do any additional setup after loading the view, typically from a nib.


-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
if (action == @selector(copy:))
return NO;

else if (action == @selector(select:))
return YES;

return [super canPerformAction:action withSender:sender];

Answer Source

The first thing you need to do is add new class with subclass of UIWEBVIEW.

Paste this code in .m file of your class.

@implementation UIWebView (Additional)

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
    BOOL superCanPerform = [super canPerformAction:action withSender:sender];
    if (superCanPerform) {
        if (action == @selector(copy:) ||
            action == @selector(paste:)||
            action == @selector(cut:)||
            action == @selector(_share:))
            return false;
    return superCanPerform;

Try this out and this will hide all the submenu required.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download