1

In my ViewController's header file I have:

@interface CaseStudyChoiceViewController : UIViewController <UIWebViewDelegate>

@property (strong, nonatomic) IBOutlet UIWebView *myWebView;

@end

Then in the .m file in the ViewDidLoad method:

[_myWebView setDelegate:self];

and finally:

#pragma mark - WebView Delegate Methods

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:)   || 
        action == @selector(paste:)  || 
        action == @selector(select:) || 
        action == @selector(cut:)    || 
        action == @selector(selectAll:))
    {
        NSLog(@"Selector is %@", NSStringFromSelector(action));
        return NO;
    }
    else
    {
        return [super canPerformAction:action withSender:sender];
    }

   //I tried the code below as well and got the same result
   /*
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController) 
    {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
   */
}

The problem I have is that this will NOT disable the Copy function. When I run this the NSLog output is:

    2013-12-30 21:39:28.794 PCO - CLS[45238:70b] Selector is cut:
    2013-12-30 21:39:28.794 PCO - CLS[45238:70b] Selector is select:
    2013-12-30 21:39:28.795 PCO - CLS[45238:70b] Selector is selectAll:
    2013-12-30 21:39:28.795 PCO - CLS[45238:70b] Selector is paste:

copy: is never presented in the canPerformAction method. Any ideas on where I can capture it?

The end result here is that when the user does a long touch in the UIWebView two options pop up, Copy and Define. I only want Define.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Scooter
  • 4,068
  • 4
  • 32
  • 47
  • After further research I also tried: `[_myWebView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none'; document.body.style.KhtmlUserSelect='none'"];` Same result. – Scooter Dec 31 '13 at 02:58
  • You can try this http://stackoverflow.com/questions/5995210/disabling-user-selection-in-uiwebview – Code Hunter Dec 31 '13 at 06:48

2 Answers2

1

it work ios 7.0 -> 9.2

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

[webView stringByEvaluatingJavaScriptFromString:
   @"document.documentElement.style.webkitTouchCallout='none';"];
}
Fattie
  • 27,874
  • 70
  • 431
  • 719
vualoaithu
  • 936
  • 10
  • 9
0

Solved!

Ok, so the solution described mid-page here: Disabling user selection in UIWebView by TPoschel works as advertised...until you load a .pdf file into your UIWebView. Loading a .pdf results in the copy/define menu popping up again once the user performs a long touch in the UIWebView.

After another bout of hair pulling I found this answer on here by Johnny Rockex and it worked like a champ. UIWebView without Copy/Paste when displaying PDF files

Many thanks to him for this easy to implement, genius solution!! In fact this should be a universal solution, and work across the board without having to use webkit.

Community
  • 1
  • 1
Scooter
  • 4,068
  • 4
  • 32
  • 47