views:

146

answers:

3

To be honest I don't know how to call it, so I'll try to describe it.

UIApplicationDelegate protocol has "application:handleOpenURL:" method. And if I implement this method in my ApplicationDelegate class, it will be called when somebody opens my urls.

details: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleOpenURL:

However, I'd like my other class (uiviewcontroller) to receive this call. To make a different example - you can create a few classes and each of them can get GPS position. Is it possible to do the same with UIApplicationDelegate protocol?

I searched a lot for this topic here, but I couldn't find any answer on how to do it. I know how to get my application delegate ([[UIApplication sharedApplication] delegate]), but it's not the case in this situation.

+1  A: 

You can always tell somebody who came to objective-c from some other object oriented language, because their first instinct is to subclass, subclass, subclass. There's not a lot of subclassing in obj-c. You CAN, obviously, but it's not how things are conventionally done, especially with things that are as one-shot-ish as UIApplicationDelegate. The more Cocoaish Way is to use categories, or sometimes to create a new NSObject subclass that contains the would-be parent class as a property.

In this case, for sure subclassing is a bad idea. the UIApplication singleton can only have one delegate property. So if you create a new UIApplicationDelegate, you've got no place to hook to it.

Instead, smarten up your one delegate's application:handleOpenURL: method to catch the URL call and load up whichever UIViewController subclass (I know, I know: exceptions) is going to handle it.

Dan Ray
Thank you for quick answer! I think I understand the correct approach now, yet my case is quite complicated. I created an uiviewcontroller with uiwebview. And i wanted to communicate between uiwebview and my application via URL calls (maybe it's completely wrong?). The problem is that this uiviewcontroller is quite deep in the view hierarchy. So, at the moment of URL call, I have a few views that have been allocated already. And the view that should handle URL call is on top of the screen. I hope you understand and could give me some more insight.
Kacper86
I assume you're in a UINavigationController here? If so, you can always use `[self.navigationController viewControllers]` to get the array of view controllers on the navigation stack. That's a pretty quick way to slice deep into the view hierarchy. Then assuming you've got your UIWebView available via a public getter (probably as a synthesized property?), you can make all the calls on it you want, right from your main AppDelegate.
Dan Ray
Thank you very much! I have a few nested UINavigationControllers, so i used NSNotification (in my case it's a little bit easier). Nonetheless, I greatly appreciate your help!
Kacper86
A: 

The simplest solution would be to use an NSNotification. This will allow you to handle the handleOpenURL call wherever you need to without creating any unnecessary coupling between your application delegate and the class you want to handle it.

In your app delegate, handle the delegate method and forward the data on using NSNotificationCenter.

- (void)application:(UIApplication *)application handleOpenURL:(NSURL *)URL
{
  [[NSNotificationCenter defaultCenter] postNotificationName:@"MyApplicationHandleOpenURLNotification" object:self userInfo:[NSDictionary dictionaryWithObject:URL forKey:@"URL"]];
}

Now, wherever you need to handle this, simply register as an observer for the notification and pull the URL out of the notification user info dictionary.

Luke Redpath
It's a good approach. Today I experimented with Key-Value Observing for the first time, which is also quite easy. Easier than digging around the view hierarchy like I advised above this morning!
Dan Ray
Thank you for your response! You're right that it's a simple solution.
Kacper86
Glad it worked. Don't forget to update your answer.
Luke Redpath
A: 

Judging by your comments to Dan Ray's answer, it sounds like you are looking for something like Three20's URL-based navigation

eliego
Thanks for your comment!
Kacper86