PasteButton doesn't work on iOS 17.2 when it is not in the menu?

This is a complete test case:

import SwiftUI
import UniformTypeIdentifiers

struct TestCase: View {
    @State private var text = "Waiting"
    
    var body: some View {
        VStack {
            PasteButton(supportedContentTypes: [.fileURL, .image, .text, .url]) { itemProviders in
                Task {
                    for itemProvider in itemProviders {
                        if itemProvider.hasItemConformingToTypeIdentifier(UTType.plainText.identifier) {
                            do {
                                let item = try await itemProvider.loadItem(forTypeIdentifier: UTType.plainText.identifier, options: nil)
                                
                                if let data = item as? Data, let txt = String(data: data, encoding: .utf8) {
                                    text = txt
                                } else if let text2 = item as? String {
                                    text = text2
                                } else if let url = item as? URL {
                                    do {
                                        text = try String(contentsOf: url, encoding: .utf8)
                                    } catch {
                                        print("Error reading file: \(error)")
                                    }
                                }

                            } catch {
                                print(error.localizedDescription)
                            }
                        }
                    }
                }
            }
            Text(text)
        }
    }
}

It works well on the macOS, worked well on iOS before 17.2 and even there this PasteButton part would work if it is placed inside the menu.

However on the iOS 17.2 iOS seems to send the file URL that generally can't be accessed. The only exception is simulator where this URL can be read by the app and thus the code above would work.

This is a huge bug and thus far I couldn't find anything regarding this. Are there any workarounds?

The first thing to do here is to remove the Task. For security reasons, all item providers must start loading their contents inside the closure (and then they can finish later). Wrapping the contents in Task causes the code to be executed later, when the payload is already unavailable.

PasteButton doesn't work on iOS 17.2 when it is not in the menu?
 
 
Q