cocoa - URL scheme - Qt and mac -


i'm trying implement custom url scheme application. i've added necessary lines info.plist. after calling specified url (eg.: myapp://) application launches.

if want handle url, i've found these steps:

@interface eventhandler : nsobject { } @end  @implementation eventhandler - (id)init {     self = [super init];      if (self) {         nslog(@"eventhandler::init");          nsnotificationcenter* defaultcenter = [nsnotificationcenter defaultcenter];         [defaultcenter addobserver:self                         selector:@selector(applicationdidfinishlaunching:) //                        name:nsapplicationwillfinishlaunchingnotification           name:nsapplicationdidfinishlaunchingnotification                         object:nil];     }     return self; }  - (void)applicationdidfinishlaunching:(nsnotification *)anotification {     nsappleeventmanager *appleeventmanager = [nsappleeventmanager sharedappleeventmanager];     [appleeventmanager seteventhandler:self andselector:@selector(handlegeturlevent:withreplyevent:) foreventclass:kinterneteventclass andeventid:kaegeturl]; }  - (void)handlegeturlevent:(nsappleeventdescriptor *)event withreplyevent:(nsappleeventdescriptor *)replyevent {     nsstring* url = [[event paramdescriptorforkeyword:keydirectobject] stringvalue];     nslog(@"%@", url); }  @end 

the above code working if application running, if url gets called , application terminated, event not caught. think because this: nsapplicationdidfinishlaunchingnotification. changing nsapplicationwillfinishlaunchingnotification causes non events caught. maybe qt handles before me, can't find workaround problem.

i trying qt-based application handle custom url scheme on mac , went down same path original poster. turns out qt4 supports url events on mac, , there's no need write objective-c code receive them. in fact reason didn't receive url events when set event handler in response nsapplicationwillfinishlaunchingnotification: qt registers own handler afterward.

when url custom scheme triggered, qt application receive fileopenevent. note qapplication instance receives event. can catch making application subclass qapplication or installing event filter on standard qapplication. opted second approach.

here's eventfilter method of custom event filter class, fileopeneventfilter. emits signal urlopened when event contains non-empty url. saves last opened url in case main window isn't initialized when event arrives (which happens in app when it's not running when custom url clicked.)

bool fileopeneventfilter::eventfilter(qobject* obj, qevent* event) {     if (event->type() == qevent::fileopen)     {         qfileopenevent* fileevent = static_cast<qfileopenevent*>(event);         if (!fileevent->url().isempty())         {             m_lasturl = fileevent->url().tostring();             emit urlopened(m_lasturl);         }         else if (!fileevent->file().isempty())         {             emit fileopened(fileevent->file());         }          return false;     }     else     {         // standard event processing         return qobject::eventfilter(obj, event);     } } 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -