UINavigationController, Button Click to Open URL in WebView, Located in Different View - iphone

So basically I want a UITableView to display a bunch of websites in the RootView. Then from there, using the UINavigationController, I would like to open a different view (WebViewController) and load the URL specified in the button, in the UIWebView.
What would be the best way to do this?
EDIT: Okay so there are basically two views. The first view is the RootViewController, which is includes the UINavigationController. Basically this will include some buttons (that have custom URL's already assigned to them) that will change & load the URL in the webView using the following.
-(IBAction)linkButton:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://URL_HERE.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
The second view will be called the "CodeView" and it will include the UIWebView object dubbed webView.
So what I'm trying to accomplish is this: After clicking on one of the buttons in the RootViewController, it will switch to the CodeView and load whatever URL has been assigned to the button itself, in the UIWebView object. Then of course, have the option to return to the RootViewController via the UINavigationController, which is just common sense when it comes to using a Navigation Controller.

Well, you have to put the root view inside a navigation controller (i suupose you are already doing this) then for each button you tap you could create a new codeview with the url and push it to the navigation controller.
This way you could have your webview inside the codeview. Did i miss something?

Related

Loading web page in UIWebView in Xcode 4.2 — how to connect IBOutlets properly and ensure the web page loads?

Most tutorials on embedding a UIWebView in an iPhone app are based on older versions of Xcode. Here's an example: http://howtomakeiphoneapps.com/uiwebview-tutorial/239/
We followed the steps in that tutorial, but the steps don't quite translate to Xcode 4.2
There is no concept of a File Owner, for instance, but there is a "storyboard."
Another question: how to link the UIWebView to the UIWebView IBOutlet?
When we add the UIWebView and connect it to the ViewController, all we see is a white screen. The web page never loads.
Could anyone share tips on loading a web page with UIWebView for Xcode 4.2?
If you're using a storyboard, the file owner is still there but it's called View Controller. So to link the UIWebView in the storyboard to the UIWebView outlet, you hold down control, then click and drag a line from the View Controller to the Web View. This is all in the 'View Controller Scene' panel to the left of the storyboard.
Note that when you first create your project from the Single View Application template, there's no need to leave the 'Use Storyboard' checkbox ticked. You might find it easier to follow these older tutorials if you don't use a storyboard.
By the way, another important checkbox, just under 'Use Storyboard', is 'Use Automatic Reference Counting'. This is a great feature, but if you have it turned on while you're following the tutorial you've linked to, you'll need to skip the part where he releases the webView instance variable.
.h file
#interface webViewViewController : UIViewController <UIWebViewDelegate>
.m file
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 640.0)];
NSURL *URL = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];
webView.delegate = self ;
[webView loadRequest:requestObj];
[self.view addSubview:webView];
webview is name of IBOutlet that I have created for UIWebView.
To make and outlet, just Contrl + drag and drop from UIWebView to your H file between #interface and # end.
Hope this will help you.
Try making a new class just for the web view code. To make a new class, right click or control+click on your project folder in the left bar while in Xcode. Choose "New File", and make a new class that is a subclass of UIViewController. Leave both checkboxes unchecked. Then, select the screen that will have the Web View in it and select the Identity inspector in the bar on the right. Change the class to the name of the class you made earlier. Use all of the code from the tutorial you found, except for the -(void)dealloc part. Connect all outlets to UI elements and you should be done. Please reply to this if you are still having problems, I'd be glad to help.
P.S. Use storyboards.

UIWebView doesn't update

I am using UIWebView loadRequest to request a new URL. The UIWebView is inside a TabBar View. When I place the request inside awakeFromNib it works. When a make a subsequent call the view does not change.
[webDisplay loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
If I place the initial request inside viewDidLoad instead of awakeFromNib it also doesn't seem to work.
I note that if I track request by delegating shouldStartLoadWithRequest this method also doesn't seem to be called by this loadRequest call.
Tabbar view controllers are loaded only once i.e when a particular tab is selected for the first time. So, viewDidLoad and awakeFromNib are only called once.
You need to call loadrequest in viewDidAppear method for the webview to be reloaded with latest content whenever that particular tab is selected.

Shared NSMutableString for local html filename iphone

I am relatively new to Objective C/iPhone Development.
I am developing a simple app with two views using a Navigation controller.
The first view contains several buttons a and the second view contains a uiwebview.
The idea is that when a button is pressed it switches to the second view and the web view loads a local html file. Which html file is loaded depends on which button was pressed.
At the moment, the view successfully changes and I can specify a html file to load in the viewdidload method of the webviewcontroller. However this obviosly means that every button triggers the same html file.
Therefore I want to create a NSMutable string which can be accessed from both views(i.e set to a particular filename in the first view and then retrieved when the seoncd view loads).
I have searched the internet for hours and trued to use global variables, the singleton method and accessing a variable in the appdelegate. However no matter which method I try to implement, the uiwebview always displays the same html file(which is the first file alphabetically)
Thanks for any help given. I greatly appreciate any suggestions.
You don't have to use an NSMutableString for this.
Add a property url, just a normal NSString, to the second view controller. Before pushing the second view controller onto the navigation stack, you set that url property to the local url you want to load.
Then, in the second view controller class, implement:
- (void)setUrl:(NSString *)newUrl {
if(url != newUrl){
[url release];
url = [newUrl retain];
// [webView load.... load the new url in the webView.
}
}
This may be beyond you current knowledge, it contains some vital Objective-C code, like properties, setters and memory management, but just test it out and see what you can do to make it work.
EDIT You don't even need this property. Just implement a method like loadUrl: in the webViewController.
- (void)loadUrl:(NSString *)url {
// [webView load.... load the URL in the webView
}
Then, before pushing the webViewController, call this method with the URL you want to load. Avoids a property and gets rid of all the additional memory management code.
if (self.myWebViewController == nil) {
//initialise
webViewController *viewWeb = [[webViewController alloc] initWithNibName:#"webViewController" bundle:[NSBundle mainBundle]];
self.myWebViewController = viewWeb;
[viewWeb release];
}
[self.myWebViewController loadUrl:#"some-local-url"];
[self.navigationController pushViewController:self.myWebViewController animated:YES];
As a note, a good practice is to capitalize each classname. Now you have a class webViewController, but in Cocoa it's common to name the class WebViewController, then you can have a variable called webViewController. Also, you can clearly see whether you are dealing with object (variable) or just a class.
EDIT Updated
So I forgot about the fact that the webView will not have been loaded until your view gets loaded. To solve it you'll have to reintroduce the property I talked about earlier. This time some adjustments are made. In your webViewController class, add the following code:
- (void)viewDidLoad {
// viewDidLoad gets automatically called once the view has loaded. Here we want to load the webView.
[self loadWebView];
}
- (void)setUrl:(NSString *)newUrl {
if(url != newUrl){
[url release];
url = [newUrl retain];
// Update the webView with the newly set URL. This will not do anything if the view hasn't been loaded, since webView will still be nil. That's why we call loadWebView again when the view gets loaded, in viewDidLoad.
[self loadWebView];
}
}
- (void)loadWebView {
// Here you'll have to load the url. You can access it using `self.url`.
//[webView load...
}
Now, in the first view controller, update [self.myWebViewController loadUrl:#"some-local-url"]; to self.myWebViewController.url = #"some-local-url";.
I assume that the web view controller is initialised from the navigation controller, so why don't you just pass it that string when it is initialised. Then when a button is pressed it uses the correct string to instigate the web view controller.
Another idea, one I think you've tried, but I am not sure, is that the web view controller has a static method and value. When you press the button it then calls the static method on the web view controller. Then when web view controller is loading up, it calls a get static method of the value.

Send UIWebView title to UINavigationBar

I am trying to send the UIWebView page Title to UINavigationBar.
I would also like it if a user clicks on a link the UINavigationBar shows a back button but if on the home page hide the back button.
to retrieve the title page into an UIWebView you can use it:
myNavigationBar.title = [myWebView stringByEvaluatingJavaScriptFromString:#"document.title"];
If you want to go back you can do that:
if ([myWebView canGoBack]){
[myWebView goBack];
}
To know if the user has loaded a new page you must set the UIWebViewDelegate. For more information see the UIWebView Class Reference and the UIWebViewDelegate Protocol Reference
As of iOS8 you should just use WKWebView and its title property.

UIViewControllers and UIWebView

I am a newbie at iPhone development and have a question, which may end up helping understand this concept in general.
Basically, I have UIViewController which loads up a view with a bunch of stuff. Now I want when a user clicks on a button, to load up a different view, which happens to be a webView. Now, I want the weview to load up a different url depending on which button was pressed in the original view.
how do i go about doing this? Basically in my head I thought i could load and swap the views when the button is pressed, like so:
In
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedLink = [valuesForSection objectAtIndex:row];
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
[self.view removeFromSuperview];
[self.view insertSubview:webView atIndex:0];
}
Is this the right way of doing it? Or how do i go about doing this?
That would be the way I'd go about it, yes. Create a single UIWebView and, depending on which cell gets selected, load a NSURLRequest into the view using loadRequest:. It has the advantages of not requiring you to build separate web views for each cell, and of being asynchronous.
However, I wouldn't necessarily remove self.view from its superview whenever a cell gets clicked; rather, I'd pop up either a modal view controller (presentModalViewController:animated:) with the web view as its view, or I'd push a new controller onto a navigation controller stack (pushViewController:animated:). It's a smoother transition and will look better to the user.
Edit (in response to comment): Yes, it's better to have another controller than to just swap views. Doing removeFromSuperview and addSubview: keeps both views within the same controller, but as a result will make your code more difficult to manage (one controller will deal with two views) and have a worse user experience (there's no transition, like is built-in with a navigation controller push).
In order to do the push properly, you should:
Build a navigation controller for your existing view controller
When you need to, create a new instance of UIViewController with its view set to the UIWebView you create and load with your HTML
Push the new view controller onto the navigation controller stack from your old view controller by calling:
[self.navigationController pushViewController:newViewController animated:YES];