skip to Main Content

I am trying to open a file from the Finder app with my Flutter app. However, the method that is expected to be called when opening a file is never being invoked.


I’ve created a custom file extension for my flutter app and already registered it in the Info.plist for MacOS. Files of the custom type are already being recognized by the operating system and are displayed with the correct icon. Double clicking one of them opens or focuses my Flutter app.

I have read online that the correct way to listen to opened files on MacOS is to override the application(_ application: NSApplication, open urls: [URL])method of FlutterAppDelegate in AppDelegate.swift. So far my code looks like this:

@NSApplicationMain
class AppDelegate: FlutterAppDelegate {
  ...  
  override func application(_ application: NSApplication, open urls: [URL]) {
    ApplicationService.shared?.methodChannel.invokeMethod("print", arguments: "Hello World!")
  }
  ...
}

But as it seems, the method is never being called. It can’t be methodChannel, as it successfully sends a print, when initialized.

I even tried removing all pub libraries from my project in order to see if any of them have an impact on the problem. Is it still possible that there is a Flutter plugin that is consuming the "OpenUrl" Event?

Any help is appreciated! Thanks 🙂

2

Answers


  1. The application(_ application: NSApplication, open urls: [URL]) might not work due to:

    1. Incorrect file association: Check "CFBundleTypeRole" and "LSUIElement" in Info.plist.
    2. URL encoding: Decode the urls using URLDecoder.
    3. Method channel error: Verify method name and arguments on both sides.

    Debug with logging and inspect values to pinpoint the issue.

    Login or Signup to reply.
    1. Try to detect the method call with something simple, e.g. with an Alert:
    override func application(_ application: NSApplication, open urls: [URL]) {
      showMessage("Hello", "world")
    }
    
    func showMessage(question: String, text: String) {
      let alert = NSAlert()
      alert.messageText = question
      alert.informativeText = text
      alert.addButton(withTitle: "OK")
      alert.alertStyle = .warning
      alert.runModal()
    }
    
    1. Make sure that your path/filename don’t contain any special characters (like mía música), again, start with some simple file in your home directory
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search