[ Click to return to the Blog Index ]

URLSchemes and UIWebViews

Calling your own app within a UIWebView…

2011 November 29

Yo dog, I heard you like URLSchemes for calling your app from Safari, so I put a UIWebView inside your app so you can call your own app from within, using URLSchemes.

Don’t ask about design or usability or even reason, you never know when you might run into that scenario, like I just did. So here’s how to get it working…

Registering your custom URLScheme is a piece of cake and there are several guides on the web. The next thing to do is to implement “handleOpenURL” in your delegate:


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

In here you can set your globals for later processing or if you – like me – need to do different things depending on where you are in the app (assuming you had it open already) then you might want to use NSNotificationCenter and implement methods for different actions where you need to. Just send out a notification and let each concerning view handle it:


[[NSNotificationCenter defaultCenter] postNotificationName:@"MyCustomNotification" object:self userInfo:[NSDictionary dictionaryWithObject:url forKey:@"URL"]];

What I did was just register an observer in the views where I needed a particular action:


[[NSNotificationCenter defaultCenter] addObserver: self selector: selector(myCustomMethod:) name: “MyCustomNotification” object: nil];

And then implement the method as a usual selector, but receiving an NSNotification object:


-(void) myCustomMethod:(NSNotification*)notification {

}

All good, right? But what happens if you have a UIWebView and you’re expected to respond to the same URLScheme from webpages viewed in that UIWebView? Well, if you don’t have it already, you need a UIView that servers as a delegate for UIWebViewDelegate, this is going to be your very own web browser and as such, you can control what links do by implementing shouldStartLoadWithRequest:


-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

Just intercept the request by parsing the [request.URL scheme] object and send out notifications like you did from your delegate!

And that’s it, you can now launch your app from Safari, or from itself using a UIWebView browser.

commenting closed for this article

flickr

About me...

My real name is Allan and i'm 22 23 24 25 26 27 28 29 30 years old.

My mind, personality and the bunch of absurd posts that you'll read here have been influenced mostly by my career (Software Engineer), my exhobbie (Electronic Sports) and my past failure success failure success in love and relationships. Get ready for some a lot of sarcasm, irony, narcissism, egocentrism and a lot of other terms that end up with "ism"..., comedy and senseless stories about my life...

Search this blog...

Meet me on...

[ Facebook.com ] [ Last.FM ] [ Del.icio.us ] [ Twitter.com ] [ Hi5.com ]
[ Flickr.com ] [ LinkedIn.com ]