I was testing out the drag-drop feature in MacOS with a GUI app (uses SwiftUI framework).
I have modified Info.plist file to accept any file on drag-drop.
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>*</string>
</array>
</dict>
</array>
DragDropV3App.swift
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
print("hello")
// C function which writes the passed argument (in this case, the string, "Hello") to a log file.
PrintToFile("Hello");
}
func application(_ sender: NSApplication, openFile filename: String) -> Bool {
PrintToFile ("File open func called!");
return true
}
}
@main
struct DragDropV3App: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
When I drag and drop a file into the .app icon, the application launches and "Hello" string is written to log file, but the string - "File open func called!", is not present in the file. The application:openFile is not called when the file is drag-dropped.
To understand basic swift, I have followed the official documentation. Some of the links I referred during this exercise are,
I have also checked other stackoverflow answers (like this) to check if this is right way. The application:openFile seems to be the right function.
Do let me know where I went wrong. Thank you.