2

I want to restrict user from copying and sharing text in UIWebView.

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];
}

2 Answers2

3

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.

Rupinder
  • 156
  • 1
  • 12
0

Try with code, This works for me

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{
     [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}

To Disable Sharing,

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { 
if (action == @selector(customMethod:)) {
    return [super canPerformAction:action withSender:sender];
}
return NO;
}

Hope this Works for you.

Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38