Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
alihassan143 opened this issue Nov 24, 2023 · 4 comments
Closed

Comments

@alihassan143
Copy link
Contributor

alihassan143 commented Nov 24, 2023

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

@cliftonlabrum
Copy link

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

It works fine in iOS.

@spacedevin
Copy link

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)
  }

@alihassan143
Copy link
Contributor Author

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

@cliftonlabrum
Copy link

cliftonlabrum commented Dec 17, 2023

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)
}

@lijy91 lijy91 closed this as completed Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants