Search code examples
iosswiftplist

Impossible to parse result for await itemProvider.loadItem for URL


My code goes like this:

let item = try await itemProvider.loadItem(forTypeIdentifier: UTType.url.identifier, options: nil)

if let data = item as? Data  {
    do {
        if let plist = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] {
            print(plist)
        } else if let url = URL(dataRepresentation: data, relativeTo: nil) {
            print(url.absoluteString)
        }
    } catch {
        print(error.localizedDescription)
    }
}

I copy and paste the URL from iOS 17 Safari using the SwiftUI PasteButton when testing this.

The plist parsing is not working, and the data gets parsed as URL, but I get something that reminds more on plist.

So I have tested for today's first story on Engadget, the test URL was: https://www.engadget.com/microsoft-surface-laptop-studio-2-hands-on-more-ports-and-a-much-needed-spec-bump-171733141.html

After parsing it as URL, I get:

bplist00%C2%A3%01%02%03_%10yhttps://www.engadget.com/microsoft-surface-laptop-studio-2-hands-on-more-ports-and-a-much-needed-spec-bump-171733141.htmlP%C3%91%04%05Utitle_%10RMicrosoft%20Surface%20Laptop%20Studio%202%20hands-on:%20More%20ports%20and%20a%20much-needed%20spec%20bump%08%0C%C2%88%C2%89%C2%8C%C2%92%00%00%00%00%00%00%01%01%00%00%00%00%00%00%00%06%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%C3%A7

Again this looks like plist, but at least with the code above doesn't parse.

It is likely that something has changed in iOS as according to this old thread simple parsing to URL should work: How do I load a URL using NSItemProvider's new Async loadItem method?

How to handle this?


Solution

  • I appreciate the help from others. The answer is that plist needs to be casted to [Any] instead of [String: Any]