16

I have an app at the moment that when a button is pushed on the first screen does some work and makes a URL, and then does

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:currentURL]];

which launches Safari with my URL. I want instead to have a webview launch from here so the user can do some custom things with it. I am still having a hell of a time understanding MVC in iOS and need help. My project is minimal and consists of an AppDelegate.h/m and a ViewController.h/m, the.m of the view controller is where the function that does this Safari launch lives.

Can anyone help me understand how to do what I'm trying to d?

Thanks...

cdietschrun
  • 1,623
  • 6
  • 23
  • 39

3 Answers3

31

The simplest way is just to add a UIWebView when the button gets pressed. Add this method to your ViewController.m and have this be performed when the button gets pressed.

Programmatically:

//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];  //Change self.view.bounds to a smaller CGRect if you don't want it to take up the whole screen
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
    [self.view addSubview:webView];
    [webView release];
}

Using Interface Builder:

1) Add a UIWebView object to your interface.

2) Set the "hidden" property to checked (in the "Attributes Inspector" window in Interface Builder). You'll keep it hidden until you want to show it.

3) Add the following code to your ViewController.h, below the other @property lines:

@property (nonatomic, retain) IBOutlet UIWebView *webView;

4) Add the following line below the @synthesize in your ViewController.m

@synthesize webView;

And add [webView release]; in the dealloc method.

5) Go back into IB and click on File's Owner, and connect the webView outlet to the webView you created.

6) Add the following method instead of the method I showed above (for the programmatic example):

//This method should get called when you want to add and load the web view
- (void)loadUIWebView
{
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]];
    self.webView.hidden = NO; 
}

You could set up an IBAction method to respond to the button press, but it sounds like you already have the button press working, so I wouldn't bother with that now.

As far as adding a button above the web view, you can either subclass web view and add the button there, or just have a separate button you define in your nib or programmatically and have that hide the webView to "get rid of it".

Ned
  • 6,280
  • 2
  • 30
  • 34
  • I don't know how to answer your IB question...I have one. I don't know if I'm using it or what. And I currently have my function that is wired up to my button in ViewController.m and I can't give it self.bounds, it says its not found on object of my type, also I can't do the self addSubView call for similar reasons... – cdietschrun Mar 23 '11 at 17:09
  • Thanks. That definitely works. 2 questions- if I want to do some setting with the WebView before I launch the link, I assume that is possible here? For example if I want some way to make it like Twitter's style where I click a link, and at the top there is a button to go back, but the rest is just a webview? And secondly, a friend says he 'recommends I switch to IB and properties' for this...any idea what that means or how I could do it? Thanks for the help – cdietschrun Mar 23 '11 at 17:21
  • I added answers to your questions. I would brush up IB a little by walking through a tutorial, it's pretty simple once you get the hang of it, but there's a bit of a learning curve. – Ned Mar 23 '11 at 17:33
  • Wow you're great, I appreciate it. It's examples like this I needed, Apple's 'examples' are poor in my opinion. As for the button I wanted, I plan on (hopefully) having a table view of links that will each eventually launch the webview with their loaded URL,and I would like a button at the top to go back to the table view and other functionality hopefully (like post this link on facebook or something...) – cdietschrun Mar 23 '11 at 17:44
  • @Ned +1 for very thorough answer. @cdietschrun Don't forget to accept Ned's post if it answered your question. – Caleb Mar 23 '11 at 18:13
  • I love that this explains the programmatic way. Too many online tutorials with nothing but screenshots of which GUI buttons to click. – Dave Dopson Mar 11 '13 at 18:01
  • Man, searched all over for an example to this seemingly easy step. Thanks for the answer. – Mike Purcell May 08 '13 at 00:13
1

For adding UIWebView using Swift to your app just drag the WebView to your story board and then using an assistant editor connect WebView to ViewController.swift And then Loading URL to WebView is very easy. Just create a WebView in your storyboard and then you can use the following code to load url.

    let url = NSURL (string: "https://www.simplifiedios.net");
    let request = NSURLRequest(URL: url!);
    webView.loadRequest(request);

As simple as that only 3 lines of codes :)

Ref: UIWebView Example

Shaba Aafreen
  • 1,497
  • 2
  • 8
  • 4
0

Since this is the top result on Google, if you can use WKWebView instead of UIWebView, you should.

import WebKit

let webViewConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: CGRect(), configuration: webViewConfiguration)
Muruganandham K
  • 5,271
  • 5
  • 34
  • 62
Alan Zeino
  • 4,406
  • 2
  • 23
  • 30