2

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.

ohadpr
  • 977
  • 1
  • 8
  • 16

2 Answers2

3

Simple:

UIWebView *webView = [[[UIWebView alloc] initWithFrame:self.view.bounds] autorelease];
webView.delegate = self;
[self.view addSubview:webView];
Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Thanks, how do you implement a custom 'shouldStartLoadWithRequest' handler to this delegate? when done do you remove the webview by just removing the subView? – ohadpr Nov 20 '10 at 20:53
  • You should really read up on the basics. Do you have beginner's book? Or try to follow some of Apple's sample projects. – Ole Begemann Nov 20 '10 at 21:15
  • will do, I just want to make sure its possible without requiring the addition of new files (e.g. for implementing the Delegate) – ohadpr Nov 20 '10 at 21:23
0

You just need to implement the WebViewDelegate's method in the controller, that you are making the delegate of the UIWebView.

  • the question clearly states that he is a beginner and giving this info alone is not what he expects or what could help anybody! – Khay Jun 13 '14 at 12:18