How to Disable Copy Paste Option from Uitextfield Programmatically

How to disable pasting in a TextField in Swift?

I agree with Leonardo Savio Dabus, if I were you I'd use string checking and just give out a warning, it makes things easier. BUT, if disabling paste option is a fancy feature you really want to put into your app, then you need to do more work. I'll provide the steps below.

Step 1: You need to create another class which extends the UITextField. In this example, I made my CustomUITextField.

import Foundation
import UIKit //Don't forget this

class CustomUITextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) {
return false
}
return super.canPerformAction(action, withSender: sender)
}
}

Step 2: Wire the storyboard with your ViewController. You need to declare an IBOutlet as in normal case:

@IBOutlet var textFieldA: CustomUITextField?

Wire the circle next to the @IBOutlet to the TextField in the storyboard. THEN, this is important and easy to be ignored:

  • Go to your storyboard
  • Click the target TextField
  • Select Identity Inspector (the 3rd one)
  • Change the class to CustomUITextField

Quick snapshot is provided below.

Sample Image

That's it, hope this works.

Credit:

Main reference

If you want to know more about the behavior of canPerformAction method, though it's an Objective-C version, the concepts are shared here.

UITextField how to disable the paste?

overrides the canPerformAction:withSender: method to return NO for actions that you don't want to allow:

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

In Above Code you need to write only for paste

Another way

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

Also check This link

EDITED

In iOS 7, you can do such like,,

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
}

For Swift User

override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(copy(_:)) || action == #selector(paste(_:)) {
return false
}

return true
}

If you want to Open Date Picker or Picker view on TEXTFIELD click then below code work.

Add below two methods in your class.

//Hide Menu View
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {

if YOURTEXTFIELD.isFirstResponder {
DispatchQueue.main.async(execute: {
(sender as? UIMenuController)?.setMenuVisible(false, animated: false)
})
return false
}

return super.canPerformAction(action, withSender: sender)
}

//MUST Implement

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}

Disable copy, paste in UITextfield is not working in iOS 9.x

Add below method in your code and that`s it,

 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
}

It will disable all types of edit.

Hope this will help :)

In iOS, how can I disable pasting into a UITextfield or other input field?

Another way is subclassing UITextField and overriding canPerformAction:withSender: method like described in Adding a dynamic custom UIMenuItem to Copy & Paste Menu before it shows

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

How to disable UITextview selection text, copy/paste UIMenuController but still have hyperlinks working [Not duplicate]

just try to create a subclass of UITextView that overrides the canPerformAction:withSender: method

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

Disable cursor and copy/paste in UITextView (swift)

I think the easy way is to make it with a button instead UITextField

Sorry - this is not for Swift it's for Obj-C but this is the idea:

To do what you want with UITextField you have to subclass the UITextField and try to use this code to disable/hide caret and input (copy/paste)

- (CGRect) caretRectForPosition:(UITextPosition*) position
{
return CGRectZero;
}

- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
return nil;
}

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

example from here: http://b2cloud.com.au/tutorial/disabling-the-caret-and-text-entry-in-uitextfields/

Anyway... this is a "functional" example: https://github.com/hackiftekhar/IQDropDownTextField



Related Topics



Leave a reply



Submit