4

My app is a ios phonegap application . I want to disable the copy and paste menu from the textfields on the web view. On long press and on double clicks , the copy paste menu shows up.I tried to disable long press and double clicks with UIGestureRecognizer class :

- (void)viewDidLoad{
UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc]     initWithTarget:self action:@selector(gestureHandler:)];
[longPressGesture setMinimumPressDuration:0.2];
longPressGesture.delegate = self;
[self.webView addGestureRecognizer:longPressGesture];
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer    shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer   *)otherGestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {
    return  NO;
    }
    else if([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]){
    return  NO;
    }
    else
        return YES;
}

But am not able to disable it for double clicks.Anyone with the same query? Help me out...

user2903299
  • 101
  • 2
  • 9

3 Answers3

5

Well you have to write a category for UIWebView that overrides the canPerformAction method,

@implementation UIWebView (DisableCopyPaste)

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController) {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
}

@end

import this category in your .pch file found in the xcode project folder, set break points to test whether the long press event fires this method.

FYI this method may be called many times, don't worry it is for the list of options that are available for the particular UI component that the user long presses.

For creating a category follow these steps.

Click the add button on the bottom of your project's solution browser in Xcode.

enter image description here

Next Select Objective C Category in the options.

enter image description here

Next Select UIWebView or type UIWebView in the category for textbox and give any name to the category enter image description here

Click Next and save the category on to your project location and copy paste the above function. Voila!.

For disabling copy paste or other options in text input placed in HTML see this.

Community
  • 1
  • 1
Satheesh
  • 10,998
  • 6
  • 50
  • 93
  • Can i know , if we can create a category for a webview in phone gap. I am new to it , so have queries with phone gap. – user2903299 Oct 21 '13 at 13:47
  • No problem, will walk you thru. – Satheesh Oct 21 '13 at 13:47
  • thanks satheeshwaram for your suggestion. I am still not able to solve the problem, i still get the same copy and paste menu on the webview – user2903299 Oct 22 '13 at 04:59
  • Did you import this category in your .pch file? Did the longpress action fire this event? – Satheesh Oct 22 '13 at 05:35
  • yes i have and the function gets hit when long press event occurs. Function returns false still the copy paste menu pops up – user2903299 Oct 22 '13 at 05:56
  • Oops sorry gave you the wrong function, try this function now. – Satheesh Oct 22 '13 at 06:01
  • i have seen this option on all ios app. Is this the behaviour of the browser/webview. Can't we disable it? My app is cordova based. I am facing issue on the webview not a view or view controller. – user2903299 Oct 22 '13 at 08:06
  • yes you can do it, I have tried this in a cordova + ios native hybrid app previously and found it working. If the method is being fired you are on the right direction but hiding the menu is our problem. Can you check whether the menuController object has some value during the break point? – Satheesh Oct 22 '13 at 08:08
  • I have tried adding this to the css -webkit-user-select: none; But this disables the input field , it doesnt take any input from the keyboard. – user2903299 Oct 22 '13 at 11:00
  • This works great for text inside a UIWebView. But do you know how to control available actions for an HTML input field inside the UIWebView? – djschwartz Dec 03 '13 at 15:59
  • Did you check whether this method was getting called when you start editing the text input inside HTML?? – Satheesh Dec 04 '13 at 05:44
1

Add following css in your web page body or on page for disable selection/copy of UIWebView

.ui-page 
{ 
   -webkit-touch-callout: none; 
   -webkit-user-select: none; 
}
Gp2mv3
  • 1,435
  • 20
  • 33
Ved
  • 2,701
  • 2
  • 22
  • 30
  • +1, this is a good way to do this but does not work all the time. – Satheesh Oct 21 '13 at 13:56
  • and also it gives some issues in ios7 http://stackoverflow.com/questions/11290613/disable-copy-and-paste-in-uiwebview-unsolved/12905995#12905995 – Satheesh Oct 21 '13 at 14:03
  • this disables my input field, keyboard shows up but cannot type anything to the field – user2903299 Oct 22 '13 at 11:03
  • Using Crosswalk (using Blink), `-webkit-touch-callout: none` has no effect, though `-webkit-user-select: none` works fine. Shame that both of them are non-standard. – Marco Scannadinari Jun 25 '14 at 18:16
1
  Use below code.
  <style type="text/css">
    *:not(input):not(textarea) {
       -webkit-user-select: none; /* disable selection/Copy of UIWebView */
       -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */
    }       
  </style>

  If you want Disable only anchor button tag use this.
   a {-webkit-user-select: none; /* disable selection/Copy of UIWebView */
      -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */
  }
Narsingh Tomar
  • 487
  • 5
  • 15