Search code examples
iphonedynamicuiwebviewuiwebviewdelegate

How do you create and manage a UIWebView object entirely in code?


I'm adding a UIWebView to my iOS App and I'd like it to open in response to a button getting clicked (so this code is going to be written in one of the button's event handler).

How can I create this UIWebView dynamically in code, position it to cover the entire screen and respond to events (e.g. to UIWEbView's shouldStartLoadWithRequest function so that the UIWebView can ask the native code to close the UIWebView).

I'd specifically like to avoid having to create stuff in Interface-Builder and it would be great if this could be reduced to several lines of code that could be later copy-pasted into other projects easily.


Solution

  • Simple:

    UIWebView *webView = [[[UIWebView alloc] initWithFrame:self.view.bounds] autorelease];
    webView.delegate = self;
    [self.view addSubview:webView];