Yogi Yogi - 10 months ago 43
iOS Question

Opening an image in a web view on clicking an image on another web view?

I am developing an iPhone app in which there are big tables to show on web view. I am planning to give a small image at the place of actual table and when the user will click on that, the actual image will open in another webView. I am looking for right direction. Please help.

Answer Source

Implement the UIWebViewDelegate protocol method shouldStartLoadWithRequest for your first webview containing your small images.

When a user clicks your image you need to intercept this event and decide what and how to do.

The shouldStartLoadWithRequest method is called when some kind of interaction is triggered in you webview, such as a link being clicked. Refer to the UIWebView documentation for more detail. The shouldStartLoadWithRequest has to return a BOOL to indicate the webview that it should proceed with the action the user performed(return YES) or not(return NO).

Now that you know that a link has been clicked you have to make sure, that it has been an image. This is done by evaluate the url passed back by the request parameter. I've made up a method clickedOnImageWithUrl in the example, which will return YES if an image has been clicked. However, I don't know what your small images embedded in a link look like I cannot provide any more detail on that method. If it returns YES you can show your new webView.

Then the code could look like this to intercept a clicked link:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)aRequest navigationType:(UIWebViewNavigationType)navigationType
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        // check if user clicked on your image...clickedOnImageWithUrl is made up
        if([self clickedOnImageWithUrl:[aRequest url]]) {

             // push new webview with image ...

             return NO;

    return YES;