Search code examples
objective-ciosxcode4

Creating a UIWebView programmatically w/delegates


I'm creating a UIWebView programmatically but I need to have this function fire so hrefs open in a custom view:

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

    //You might need to set up a interceptLinks-Bool since you don't want to intercept the initial loading of the content
    if (interceptLinks) {
        NSURL *url = request.URL;
        //This launches your custom ViewController, replace it with your initialization-code
        BrowserViewController *bv = [[BrowserViewController alloc] initWithUrl:url];
        bv.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentModalViewController:bv animated:YES];
        [bv release];
         return NO;
    }
    //No need to intercept the initial request to fill the WebView
    else {
        interceptLinks = YES;
        return YES;
    }
}

How do I link delegates to a UIWebView created programmactically so the functions will fire?


Solution

  • What you are doing above looks correct.

    Just add UIWebViewDelegate protocol on your interface class, and on your implementation class add

    webview.delegate = self;
    

    So your shouldStartLoadWithRequest is called.