How to add Paste button in UIMenu such that the system "allow app to paste" prompt does not appear

Apps that try to access the contents of the pasteboard cause a system prompt to appear asking the user

"AppName" would like to paste from "OtherAppName"

Do you want to allow this?

Don't Allow Paste

Allow Paste

This prompt does not appear if you implement a UIPasteControl and the user taps it to signal intent to paste, but this control cannot be placed into a UIMenu. I read this could be achieved with UIAction.Identifiers like .paste or .newFromPasteboard but the prompt still appears with the following code. What's the trick?

override func viewDidLoad() {
    super.viewDidLoad()
    
    title = "TestPaste"
    view.backgroundColor = .systemBackground
    
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.contentMode = .scaleAspectFit
    imageView.clipsToBounds = true
    view.addSubview(imageView)
    NSLayoutConstraint.activate([
        imageView.topAnchor.constraint(equalTo: view.topAnchor),
        imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    ])

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", image: UIImage(systemName: "plus"), menu: UIMenu(children: [
        UIAction(identifier: .paste) { _ in
            imageView.image = UIPasteboard.general.image
        }
    ]))
}

Hello Jordan,

If you use the standard UIAction.Identifier.paste identifier, it should enable secure paste for that menu item while suppressing the paste prompt. However, please note that certain actions will still cause the Paste prompt to appear, such as pressing the button and holding it down, which summons the menu, then sliding the finger over Paste and then finally lifting. Or selecting that item with the keyboard.

Hoping this helps,

Richard Yeh  Developer Technical Support

Hi @DTS Engineer! In the code above that uses UIAction.Identifier.paste, the paste prompt is not suppressed when I tap Add and then tap Paste. Is that unexpected behavior or have I misunderstood in what scenario it would be suppressed?

Hello Jordan,

Your code should prevent the paste prompt if you're tapping on the menu item, as opposed to other touch interactions such as sliding, or keyboard selection. Do you get the same results with just the relevant code in a small test project? If so, please share a link to your focused sample project. That'll help us better understand what's going on. If you're not familiar with preparing a test project, take a look at Creating a test project.

You could also create a bug report on Feedback Assistant and attach a sysdiagnose so we can determine if this is an unexpected issue. If you do create a bug report, please post the feedback report number to this thread so I can track it accordingly.

Thank you,

Richard Yeh  Developer Technical Support

Thanks @DTS Engineer! I can confirm it does work when tapping (using the latest iOS beta), and not in the scenarios you outlined. I swear I've seen the prompt appear upon simply tapping the action but I cannot replicate now. I did file feedback (FB22485164) requesting a pasteboard constant be added to UIAction.Identifier to get an action titled Clipboard to fit in my menu UI. Paste (or New from Clipboard) doesn't thrive here:

How to add Paste button in UIMenu such that the system "allow app to paste" prompt does not appear
 
 
Q