Skip to content

Protocol Handler not working with flutter version 3.16.0 in Macos #12

Closed

Description

@alihassan143

the protocol listeners did'nt work even thought the cusotm uri permission is added in info.plist file

Activity

  1. cliftonlabrum commented on Nov 24, 2023

    @cliftonlabrum

    I'm seeing the same issue. onProtocolUrlReceived doesn't get called now.

    It works fine in iOS.

  2. spacedevin commented on Nov 27, 2023

    @spacedevin

    Moving the listener to AppDelegate worked for me for now on 3.16.0

    https://github.com/knoeone/soundsetr/blob/main/macos/Runner/AppDelegate.swift#L14

      override func applicationWillFinishLaunching(_ notification: Notification) {
        super.applicationWillFinishLaunching(notification)
          NSAppleEventManager.shared().setEventHandler(self, andSelector:#selector(handleURLEvent(_:with:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
      }
    
      @objc public func handleURLEvent(_ event: NSAppleEventDescriptor, with replyEvent: NSAppleEventDescriptor) {
        guard let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue else { return }
        
          let window = self.mainFlutterWindow?.contentViewController as! MacOSWindowUtilsViewController
        let messenger = window.flutterViewController.engine.binaryMessenger
        let channel = FlutterMethodChannel(name: "protocol_handler", binaryMessenger: messenger)
        let args: NSDictionary = [
            "url": urlString,
        ]
        channel.invokeMethod("onProtocolUrlReceived", arguments: args)
      }
  3. alihassan143 commented on Nov 28, 2023

    @alihassan143
    ContributorAuthor

    Moving the listener to AppDelegate worked for me for now on 3.16.0

    https://github.com/knoeone/soundsetr/blob/main/macos/Runner/AppDelegate.swift#L14

      override func applicationWillFinishLaunching(_ notification: Notification) {
        super.applicationWillFinishLaunching(notification)
          NSAppleEventManager.shared().setEventHandler(self, andSelector:#selector(handleURLEvent(_:with:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
      }
    
      @objc public func handleURLEvent(_ event: NSAppleEventDescriptor, with replyEvent: NSAppleEventDescriptor) {
        guard let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue else { return }
        
          let window = self.mainFlutterWindow?.contentViewController as! MacOSWindowUtilsViewController
        let messenger = window.flutterViewController.engine.binaryMessenger
        let channel = FlutterMethodChannel(name: "protocol_handler", binaryMessenger: messenger)
        let args: NSDictionary = [
            "url": urlString,
        ]
        channel.invokeMethod("onProtocolUrlReceived", arguments: args)
      }

    its working but its not the best solution we need to fix this in plugin side

  4. cliftonlabrum commented on Dec 17, 2023

    @cliftonlabrum

    Thanks, @spacedevin! Also, if you are not using the macos_window_utils dependency, you will need to modify the handleURLEvent function to look like this:

    @objc public func handleURLEvent(_ event: NSAppleEventDescriptor, with replyEvent: NSAppleEventDescriptor) {
      guard let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue else { return }
      
      //These two lines are different
      guard let window = NSApplication.shared.windows.first else{ return }
      let controller = window.contentViewController as! FlutterViewController
    
      let messenger = controller.engine.binaryMessenger
      let channel = FlutterMethodChannel(name: "protocol_handler", binaryMessenger: messenger)
      let args: NSDictionary = [
          "url": urlString,
      ]
      channel.invokeMethod("onProtocolUrlReceived", arguments: args)
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions