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?

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