How does the new UIResponderStandardEditActions.pasteAndGo(_:) work?

613 Views Asked by At

In iOS 15, the following actions have been added to UIResponderStandardEditActions:

- (void)pasteAndMatchStyle:(nullable id)sender API_AVAILABLE(ios(15.0));
- (void)pasteAndGo:(nullable id)sender API_AVAILABLE(ios(15.0));
- (void)pasteAndSearch:(nullable id)sender API_AVAILABLE(ios(15.0));

In https://developer.apple.com/wwdc21/10059 it is explained that these additional actions are specifically meant to avoid the "Copy & Paste Banner" that notifies the user that the app accessed the pasteboard. Since these are user initiated actions, the OS now knows that the intent was to paste so there is no need to notify the user.

However, I cannot get this to work.

I tried to do the following:

class ViewController: UIViewController {
    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let item = UIMenuItem(title: "Paste & Go", action: #selector(pasteAndGo(_:)))
        UIMenuController.shared.menuItems = [item]
    }
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(pasteAndGo(_:)) {
            return true
        }
        return super.canPerformAction(action, withSender: sender)
    }
    
    override func pasteAndGo(_ sender: Any?) {
        textField.pasteAndGo(sender)
    }
}

Basically, I add it as a custom item to the UIMenuController, announce that I can handle pasteAndGo(_:) and then delegate it to the UITextField when the menu item is selected.

The menu item shows up, but when selected, it causes an exception telling me that UITextField does not respond to the pasteAndGo: selector:

Thread 1: "-[UITextField pasteAndGo:]: unrecognized selector sent to instance 0x14d813800"

I don't understand this part. Isn't pasteAndGo(_:) supposed to be aliased to paste(_:) inside the UITextField? I thought that since these are aliases, we can get the same behaviour (text is pasted) but now we know in our actions what the actual intent was so that we can act on that accordingly.

I've tried to set the keyboard type to "Web Search" and the Return Key to "Go" but that also made no difference.

Has anyone made this work? I'm starting to think that this may not have actually shipped in iOS 15.

0

There are 0 best solutions below