Post

Replies

Boosts

Views

Activity

Reading contents of file dropped in iOS/iPadOS/Mac Catalyst app.
I am currently refactoring my existing (Swift) iOS codebase to run on macOS under Mac Catalyst but am having trouble with reading, loading or even seeing JSON files that are received by my UIDropInteractionDelegate. I am trying to allow users to drop a file snse.json (which is a regular pretty-printed JSON text file) onto a view in my app, but in performDrop, session.items is a single item array with seemingly nothing useful in it. When I throw a breakpoint in performDrop, I get the following debug output: ▿ Optional<NSItemProvider> &#9;- some : <NSItemProvider: 0x600003876ca0> {types = ( &#9;&#9;"public.json", &#9;&#9;"com.apple.finder.node" )} (lldb) po session.items.first?.itemProvider.suggestedName ▿ Optional<String> &#9;- some : "snse.json" (lldb) po session.items.first?.localObject nil (lldb) po session.items.first?.previewProvider nil (lldb) po session.items.first?.itemProvider.canLoadObject(ofClass: URL.self) ▿ Optional<Bool> &#9;- some : false (lldb) po session.items.first?.itemProvider.canLoadObject(ofClass: String.self) ▿ Optional<Bool> &#9;- some : false Below is the code I have so far: class SentimentViewController: UITableViewController { &#9;&#9;override func viewDidLoad() { &#9;&#9;&#9;&#9;super.viewDidLoad() &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;self.view.interactions.append(UIDropInteraction(delegate: self)) &#9;&#9;} } extension SentimentViewController: UIDropInteractionDelegate { &#9;&#9; &#9;&#9;static let JSONTypeIdentifier = "public.json" &#9;&#9; &#9;&#9;func dropInteraction(_ interaction: UIDropInteraction, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; canHandle session: UIDropSession) -> Bool { &#9;&#9;&#9;&#9;return session.hasItemsConforming(toTypeIdentifiers: [JSONTypeIdentifier]) &#9;&#9;} &#9;&#9;func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal { &#9;&#9;&#9;&#9;return UIDropProposal(operation: .copy) &#9;&#9;} &#9;&#9;func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) { &#9;&#9;&#9;&#9;/* This is called with an empty array of NSURLs */ &#9;&#9;&#9;&#9;let _ = session.loadObjects(ofClass: URL.self) { urls in &#9;&#9;&#9;&#9;&#9;&#9;for url in urls { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.importJSONData(from: url) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print(url) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9; &#9;&#9;private func importJSONData(from url: URL) { &#9;&#9;&#9;&#9;print("I would love to load data from \(url).") &#9;&#9;} } I'm sure I'm doing something wrong, but I just can't tell what. Do I need to request permissions to read local files? Is there a step I missed? Any help is greatly appreciated!
2
0
1.4k
Nov ’20