禁用复制粘贴选项

禁用复制粘贴选项

我正在使用Xcode 9和iOS 11,谷歌搜索禁用复制粘贴选项,但未成功。

ios - iOS 11无法禁用复制粘贴选项UITextField-LMLPHP

下面是我尝试过的:

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

    return true
}

并且还尝试了:
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(paste(_:)) || action == #selector(cut(_:)) || action == #selector(selectAll(_:)) || action == #selector(select(_:)){
            return false
        }

        return true
    }

并尝试了Stackoverflow的许多其他代码。但是没有成功。其实我不想在菜单上方显示,然后长按UITextField

最佳答案

经过测试并在iOS 12中工作:

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool
{
    if  action == #selector(self.paste(_:))
        ||
        action == #selector(self.copy(_:))
        ||
        action == #selector(self.cut(_:))
        ||
        action == #selector(self.select(_:))
        ||
        action == #selector(self.selectAll(_:))

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

07-24 09:55