How to disable clipboard features in iOS?

How do I disable the clipboard features so that when a user taps in a text field, iOS doesn't bring up the menu to cut or copy?

Accepted Reply

You could subclass UITextView and disable the associated actions:


import UIKit

class NoActionTextField: UITextField {
   
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // write code here what ever you want to change property for textfeild.
    }
   
    override func caretRect(for position: UITextPosition) -> CGRect {
        return CGRect.zero
    }
   
    override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
        return []
    }
   
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.selectAll(_:)) || action == #selector(UIResponderStandardEditActions.paste(_:)) {
            return false
        }
        // Default
        return super.canPerformAction(action, withSender: sender)
    }
}

Credit:

https://stackoverflow.com/questions/28419861/disable-cursor-and-copy-paste-in-uitextview-swift

with minor change: selectionRects must not return [Any]


Note: I tested, it works. But you loose the insertion point visualization, which may be problematic.


If you want to hide all popup menus items but keep the caret, try the following:


class NoActionTextField: UITextField {
   
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
   
//    override func caretRect(for position: UITextPosition) -> CGRect {
//        return CGRect.zero
//    }
   
    override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
        return []
    }
   
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
         return false 
    }
}



Of course, declare the textFiel in IB as NoActionTextField

Replies

You could subclass UITextView and disable the associated actions:


import UIKit

class NoActionTextField: UITextField {
   
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // write code here what ever you want to change property for textfeild.
    }
   
    override func caretRect(for position: UITextPosition) -> CGRect {
        return CGRect.zero
    }
   
    override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
        return []
    }
   
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.selectAll(_:)) || action == #selector(UIResponderStandardEditActions.paste(_:)) {
            return false
        }
        // Default
        return super.canPerformAction(action, withSender: sender)
    }
}

Credit:

https://stackoverflow.com/questions/28419861/disable-cursor-and-copy-paste-in-uitextview-swift

with minor change: selectionRects must not return [Any]


Note: I tested, it works. But you loose the insertion point visualization, which may be problematic.


If you want to hide all popup menus items but keep the caret, try the following:


class NoActionTextField: UITextField {
   
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
   
//    override func caretRect(for position: UITextPosition) -> CGRect {
//        return CGRect.zero
//    }
   
    override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
        return []
    }
   
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
         return false 
    }
}



Of course, declare the textFiel in IB as NoActionTextField