runtime icon indicating copy to clipboard operation
runtime copied to clipboard

MacOSx Binary doesn't support URL Scheme Invocations entry point

Open josegomez opened this issue 6 years ago • 8 comments

MacOS Binary doesn't support URL Scheme Invocations entry point

Version: DotNet Core 3.1 OS: MacOSX Catalina

On most Operating Systems you can invoke a program via a specified URL Scheme. Such as "myAppScheme://" This is registered with the operating system and when the url matching the scheme is invoked the application runs. This works with self contained deployment for Linux and Windows just fine, you just register the scheme and binary in the system and it works as intended.

However with MacOSx even if you register the scheme in the system and create a .app bundle containing the appropriate registration. The application will be invoked (as expected) however the originating URL is not passed as a command line arg[] into Program.cs like it is with Linux and Windows.

This stems from the fact that MacOSx has a specific application invocation / function that gets called when the application is launched from a url Scheme which differs from the standard Entry Point of a regularly invoked application

func application(_ application: UIApplication,
                 open url: URL,
                 options: [UIApplicationOpenURLOptionsKey : Any] = [:] ) -> Bool {
    
    // Determine who sent the URL.
    let sendingAppID = options[.sourceApplication]
    print("source application = \(sendingAppID ?? "Unknown")")
    
    // Process the URL.
    guard let components = NSURLComponents(url: url, resolvingAgainstBaseURL: true),
        let albumPath = components.path,
        let params = components.queryItems else {
            print("Invalid URL or album path missing")
            return false
    }
    
    if let photoIndex = params.first(where: { $0.name == "index" })?.value {
        print("albumPath = \(albumPath)")
        print("photoIndex = \(photoIndex)")
        return true
    } else {
        print("Photo index missing")
        return false
    }
}

The dotnet core runtime in MacOSx should support this entry point and forward the entry into the standard args[] entry point in the core application.

See: Apple Documentation for reference.

josegomez avatar Jan 31 '20 13:01 josegomez

Are there any known work-arounds at the present moment?

616E64726173 avatar Feb 14 '20 21:02 616E64726173

It's not a "standard entry point". It's not an entry point at all. What you have mentioned is a selector of UIApplicationDelegate https://developer.apple.com/documentation/uikit/uiapplication/1648685-openurl?language=objc from iOS.

The corresponding MacOS API is application:openURLs: selector of NSApplicationDelegate. https://developer.apple.com/documentation/appkit/nsapplicationdelegate/2887193-application?language=objc.

That selector is called by Cocoa from the main event loop (i. e. after application calls [[NSApplication sharedApplication] run] or [NSApp run] either directly or through NSApplicationMain convenience helper).

It's not, by any means, the job of the runtime to start Cocoa event loop. Even native Objective C applications start the event loop manually.

So, to get the url, you need to register an application delegate and start the event loop of your app. With .NET Core that could be done through netstandard build of MonoMac, you can grab one here.

kekekeks avatar Feb 14 '20 21:02 kekekeks

@616E64726173 I got around it using AppleScript which supports this, so I register the schema with AppleScript script, and then use that Script to invoke my Dotnet core app passing the standard args.

josegomez avatar Feb 14 '20 22:02 josegomez

So, to get the url, you need to register an application delegate and start the event loop of your app. With .NET Core that could be done through netstandard build of MonoMac, you can grab one here.

@kekekeks can you expand on that a bit? I'm not following what you mean. I have a .net core console app and I can create the Mac Application to register the URL schema and have it invoke my app that all works. Where does the netstandard build of MonoMac come in?

josegomez avatar Feb 14 '20 22:02 josegomez

OSX doesn't pass the Uri to the entry point. Instead it waits for the app to initialize and start communications. Only then it passes the uri. It's not possible to know if your app was started with an uri or not. You need to actually start NSApplication to get the uri in a callback.

kekekeks avatar Feb 14 '20 22:02 kekekeks

Got it @kekekeks Thank you and thank you @616E64726173 I got it to work using ya'll suggestions. However I would still argue that if dotnet core is going to provide Trimmed, Single File "Mac" Applications it should provide a way for us to register intent on this particular event (and most events that a standard Mac app can handle), specially if the only viable solution with .net core is using MonoMac which is deprecated.

josegomez avatar Feb 29 '20 16:02 josegomez

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.