Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X URL handler, but when launching the application?

Tags:

macos

cocoa

I've set a URL handler "soon" when my app is launching: in -(void)applicationDidFinishLaunching:(NSNotification*) :

[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(getUrl:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
LSSetDefaultHandlerForURLScheme((CFStringRef)@"myScheme", (__bridge CFStringRef)[[NSBundle mainBundle] bundleIdentifier]);

It works as expected, the getUrl:withReplyEvent: is called whenever an URL with myScheme:// scheme is opened!

But if my app is not running (quit), if in Safari I open an URL such as myScheme://some_valuable_info?action=doSomething, it launched my app... but how do i know I've been launched by a URL handler? How can I get the URL that trigger my application launch?

like image 681
Altimac Avatar asked Oct 15 '25 11:10

Altimac


1 Answers

Responding to myself:

The eventHandler must be registered in the applicationWillFinishLaunching: , not in applicationDidFinishLaunching:

If the app is launched due to an URL handler, you'll still get the evenHandler callback, but only if you registered it very soon in the launching process.

like image 179
Altimac Avatar answered Oct 18 '25 11:10

Altimac