How do I troubleshoot my custom URL scheme?

I set up a simple event handler as mentioned here , but it looks like the selector is not being called. I put the code in my AppDelegate class and hooked up the delegate in IB. Tried putting some NSLog () s and breakpoints in the selector I expect to be called, but none of them hit. The URL scheme works as it launches my application, but nothing happens after that. Can anyone advise on how to fix this issue? Thank!

+1


source to share


4 answers


Well, I can't help but notice that your method -init

is incorrectly declared. If must have return type id

and have return self;

at the end.

- (id)init
{
    self = [super init];
    if (self) {
        [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(getUrl:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
    }
    return self;
}

      



With these fixes, I was able to inject these two procedures into the AppController test class and print the URLs (using a custom scheme) that I entered into Safari. I would put a breakpoint on this -init method and step over it to absolutely make sure that the call to the -setEventHandler: method gets called.

+2


source


The big question is, where do you call NSAppleEventManager -setEventHandler: ...? You need to call this before the application finishes launching if you want to catch the URL that launched your application. If your application delegate is created in your MainMenu.nib, then its -init or -awakeFromNib methods will work, but, for example, -applicationDidFinishLaunching: will not.

Also, make sure the selector you specify for -setEventHandler: matches the name of your method exactly, paying particular attention to capitalization and the correct number of colons.



Obviously, if you posted the appropriate application delegate code, it would be very helpful.

0


source


Thanks for the suggestions. I have double checked these things. I'm sure this is a beginner's mistake, but I would be glad if someone had a look at the code. (The bits of the URL are stored in the info.plist file.) Right now I'm just trying to confirm that it works before I try to do anything with the URL.

- (void)init{
    self = [super init];
    if(self){
        [[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(getUrl:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
    }
}

- (void)getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{    
    NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    NSLog(url);
    // now you can create an NSURL and grab the necessary parts
}

      

0


source


Newbie error # 2: Didn't set my application delegate class in IB. Fixing this and the init method as above got me going. Hmm ...

0


source







All Articles