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:
Below is the code I have so far:
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!
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:
Code Block (lldb) po session.items.first?.itemProvider ▿ Optional<NSItemProvider> - some : <NSItemProvider: 0x600003876ca0> {types = ( "public.json", "com.apple.finder.node" )} (lldb) po session.items.first?.itemProvider.suggestedName ▿ Optional<String> - 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> - some : false (lldb) po session.items.first?.itemProvider.canLoadObject(ofClass: String.self) ▿ Optional<Bool> - some : false
Below is the code I have so far:
Code Block swift class SentimentViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() self.view.interactions.append(UIDropInteraction(delegate: self)) } } extension SentimentViewController: UIDropInteractionDelegate { static let JSONTypeIdentifier = "public.json" func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool { return session.hasItemsConforming(toTypeIdentifiers: [JSONTypeIdentifier]) } func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal { return UIDropProposal(operation: .copy) } func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) { /* This is called with an empty array of NSURLs */ let _ = session.loadObjects(ofClass: URL.self) { urls in for url in urls { self.importJSONData(from: url) print(url) } } } private func importJSONData(from url: URL) { print("I would love to load data from \(url).") } }
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!