I'm trying to use the drag and drop methods to drag an .mp3 file from finder into my app (using mac catalyst). So far I have succeeded in getting (I guess) the raw data from the mp3 file into an array, but I'm not sure how to proceed. What I ultimately want is to play the .mp3 file that is dropped into the app with AVAudioPlayerNodes. Is this the right way, or should I try to get the URL of the file instead and then copy it to my app? Any help would be awesome. Thank you.
Code : Delegate methods
func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
if session.localDragSession == nil {
print("yes")
return UIDropProposal(operation: .copy)
}
print("no")
return UIDropProposal(operation: .cancel)
}
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
print("somethng happened")
session.loadObjects(ofClass: MP3Class.self) { objects in
for url in objects as! [MP3Class] {
print(url)
print(objects)
}
}
}
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
return session.canLoadObjects(ofClass: MP3Class.self)
}
func customEnableDropping(on view: UIView, dropInteractionDelegate: UIDropInteractionDelegate) {
let dropInteraction = UIDropInteraction(delegate: dropInteractionDelegate)
view.addInteraction(dropInteraction)
}
Custom mp3 class :
class MP3Class: NSObject, NSItemProviderReading {
let data: Data?
required init(mp3Data: Data, typeIdentifier: String) {
data = mp3Data
}
static var readableTypeIdentifiersForItemProvider: [String] {
return [kUTTypeMP3 as String]
}
static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
return self.init(mp3Data: data, typeIdentifier: typeIdentifier)
}
}