Search code examples
iphoneiosthree20

Is there anyway to create a link via a webview that opens a part of your application?


For example: The facebook app is basically just a webview using html5, so how does it link to things like the default message editor when you try to write something on someone's wall? I know part of the app is probably using Three20. So are they just linking to that part of the app using their app url? (fb://profile/122605446 for example).


Solution

  • Yes you can do this using the UIWebView delegate method webView:shouldStartLoadWithRequest:navigationType: method. You will need to use a custom URL scheme to trigger your custom code, otherwise you let it pass through as normal. so in your example fb is the custom scheme. The use the rest of the URL to navigate to the profile screen for user 122605446.

    Here is some sample code to help:

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
        NSURL *url = request.URL;
        NSString *scheme = [url scheme];
        if ([scheme isEqualToString:@"fb"]) {
            //Do something with the rest of the url and take any action you like.
        }
        else {
            return YES;    
        }
    }