How to disable copy paste option from UITextField programmatically How to disable copy paste option from UITextField programmatically ios ios

How to disable copy paste option from UITextField programmatically


This post has many nice solutions: How disable Copy, Cut, Select, Select All in UITextView

My favourite is to override canPerformAction:withSender::

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


Storyboard users may want to look at this solution, as long as you are ok with subclassing.

I don't think that there is an easy way to achieve this through extensions or protocols.

Swift 3.1

import UIKit@IBDesignableclass CustomTextField: UITextField {    @IBInspectable var isPasteEnabled: Bool = true    @IBInspectable var isSelectEnabled: Bool = true    @IBInspectable var isSelectAllEnabled: Bool = true    @IBInspectable var isCopyEnabled: Bool = true    @IBInspectable var isCutEnabled: Bool = true    @IBInspectable var isDeleteEnabled: Bool = true    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {        switch action {        case #selector(UIResponderStandardEditActions.paste(_:)) where !isPasteEnabled,             #selector(UIResponderStandardEditActions.select(_:)) where !isSelectEnabled,             #selector(UIResponderStandardEditActions.selectAll(_:)) where !isSelectAllEnabled,             #selector(UIResponderStandardEditActions.copy(_:)) where !isCopyEnabled,             #selector(UIResponderStandardEditActions.cut(_:)) where !isCutEnabled,             #selector(UIResponderStandardEditActions.delete(_:)) where !isDeleteEnabled:            return false        default:            //return true : this is not correct            return super.canPerformAction(action, withSender: sender)        }    }}

Gist link


For iOS8.0+, Xcode 6.0.1, ARC enabled

Hoping to save a beginner, like myself, some time implementing this...

To implement disabling copy/paste/cut/etc. you must subclass UITextField and override...

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender

To do this...

Create a new class that is a subclass of UITextField (i.e. a new .h and .m files to be included within your app folder). So File->New->"Cocoa Touch Class"->Next->"PasteOnlyUITextField" (for example), subclass of "UITextField"->Next->Create.

Once the .h and .m files are created for our new subclass of UITextField called "PasteOnlyUITextField"...

PasteOnlyUITextField.h

#import <UIKit/UIKit.h>@interface PasteOnlyUITextField : UITextField@end

PasteOnlyUITextField.m

#import "PasteOnlyUITextField.h"@implementation PasteOnlyUITextField/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {    // Drawing code}*/- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{    if (action == @selector(paste:))    {        return true;    }    return false;}@end

Now make sure you import PasteOnlyUITextField.h where you are going to use it, e.g. YourUIViewController.h file...

#import "PasteOnlyUITextField.h"

Now you must use the subclass, either progrommatically or with identity inspector

PasteOnlyUITextField *pasteOnlyUITextField = [[PasteOnlyUITextField alloc] init...];

or...

Select the UITextField and go to the identity inspector, select its class.

identity inspector

You can change the logic associated with the menu options as you see fit...

Hope this helps! Thanks to all the original contributors.