0

I need to know whether the app was launched via an URL in DidFinishLaunching.

The notification parameter in DidFinishLaunching contains a key LaunchIsDefaultLaunchKey which according to Apple documentation :

"The value is NO if the app was launched to open or print a file, to perform a Service action, if the app had saved state that will be restored, or if the app launch was in some other sense not a default launch"

This covers the case of the app being opened by an URL but it also covers cases that I don't need.

On iOS there is DidFinishLaunching with launchOptions dictionary which contains the URL in the LaunchOptionsUrlKey key.

Is there something similar to this on macOs?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Petru Lutenco
  • 217
  • 3
  • 11

1 Answers1

0

In Objective-C here's what you do.

Register to handle shared event with the class kInternetEventClass and ID kAEGetURL:

[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
    andSelector:@selector(handleURLEvent:withReplyEvent:)
    forEventClass:kInternetEventClass
    andEventID:kAEGetURL];

The callback looks like this:

- (void)handleURLEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
    // Read the URL used to launch the application
    NSString* launchUrl = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
}

You can register the callback in applicationWillFinishLaunching:, and it will be invoked before applicationDidFinishLaunching:.

The Xamarin / C# should look something like this (untested):

public override void FinishedLaunching(NSObject notification)
{
    NSAppleEventManager appleEventManager = NSAppleEventManager.SharedAppleEventManager;

    appleEventManager.SetEventHandler(this, new Selector("handleGetURLEvent:withReplyEvent:"), AEEventClass.Internet, AEEventID.GetUrl);
}

[Export("handleGetURLEvent:withReplyEvent:")]
private void HandleGetURLEvent(NSAppleEventDescriptor descriptor, NSAppleEventDescriptor replyEvent)
{

}
TheNextman
  • 12,428
  • 2
  • 36
  • 75