How do you REALLY remove Copy from UIMenuController
The technique you linked to still seems to work. I implemented a UIWebView
subclass with these methods, and only the A and B items appeared.
+ (void)initialize{ UIMenuItem *itemA = [[UIMenuItem alloc] initWithTitle:@"A" action:@selector(a:)]; UIMenuItem *itemB = [[UIMenuItem alloc] initWithTitle:@"B" action:@selector(b:)]; [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:itemA, itemB, nil]]; [itemA release]; [itemB release];}- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{ BOOL can = [super canPerformAction:action withSender:sender]; if (action == @selector(a:) || action == @selector(b:)) { can = YES; } if (action == @selector(copy:)) { can = NO; } NSLog(@"%@ perform action %@ with sender %@.", can ? @"can" : @"cannot", NSStringFromSelector(action), sender); return can;}
for ios >= 5.1 canPerformAction:(SEL)action withSender:(id)sender is not working anymore.
If you are ok with just disable paste action here is a method:
add UITextFieldDelegate to you view controller and implement method like this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{if(textField == txtEmailRe) return ((string.length) > 1 ? NO : YES);}
it means that if user enter more than one character for each action (it means that probably user is pasting something.) do not accept it in textfield.
it is a good practice for force user enter textfields like e-mail and
lemnar's answer is correct. Implementing a subclass of UIWebView works just fine. This example is OK for a UITextView. For a UIWebView, create a custom subclass as follows:
//// MyUIWebView.h//#import <UIKit/UIKit.h>@interface MyUIWebView : UIWebView@end
And:
//// MyUIWebView.m//#import "MyUIWebView.h"@implementation MyUIWebView-(BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(copy:)) return NO; else // logic to show or hide other things}@end
Then, instead of instantiating UIWebView, use MyUIWebView.
UPDATE:
If wanting to disable "copy" but leave "define" (and "translate",) which can be useful, this is how to do it; replace canPerformAction:withSender
above with this:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(defineSelection:)) { return YES; } else if (action == @selector(translateSelection:)) { return YES; } else if (action == @selector(copy:)) { return NO; } return [super canPerformAction:action withSender:sender];}