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...
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.
Next Select Objective C Category in the options.
Next Select UIWebView or type UIWebView in the category for textbox and give any name to the category
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.
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 */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With