UIWebView in a UINavigationController - iphone

How can I control the title which appears in the UINavigationController with a web page which appears in a UIWebView?
I am trying to get the UINavigationController title to reflect the title of the web page.

In your webViewDidFinishLoad method, you'll need to call something like
NSString * result = [myWebView stringByEvaluatingJavaScriptFromString:#"document.title"];
Then you'll set up the navigation controller title:
self.navigationItem.title=result;
Unless I'm missing something from your problem description, this should work.

Related

UIWebView error

I am developing one application.In that first page having one UIWebView.And i create a property for that one.And i created a object for that class in another class and give the html data for that webview like as below.But it didn't show the data on UIWebView.Please tell me how to do that one.
ViewController *view=[[ViewController alloc]init];
NSString *result1 = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];
NSString *htmldata=[NSString stringWithFormat:#"<html>%#</html>",result1];
[view.web loadHTMLString:htmldata baseURL:nil];
This result1 string contain html data without html tag.That' why iam adding that tags.
Hope you had called pushViewController or popViewController to present the view or added the view object's view as a subview of the current view. Then make sure that the webview is allocated, given a frame and added as a subview of ViewController. You did not used the method initWithNibName:, therefor, if the webview is created from xib, it will not take.

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

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?

how to open an in app browser like the mailcomposion popup

is it possible to open an in-app browser that loads a pdf and the takes you back to the nib just like the mailcomposition does with the in-app email? If so, can anyone please help.
You should be able to use a UIWebView controlled by a custom UIViewController, which you present using the presentModalViewController:animated: method in the view controller that wants to pop up the PDF.
1) Create a .xib and corresponding view controller (both named WebViewViewController here). This should include a UIWebView (to display the PDF) and a UIButton (to close the modal view & return to your original view).
2) Present your WebViewViewController as a modal view. This will "pop-over" your current view with the new view.
WebViewViewController *webViewVC = [[WebViewViewController alloc] initWithNibName:#"WebViewViewController" bundle:nil];
//I am showing it over a Navigation Controller, you may be using something different.
[self.navigationController presentModalViewController:webViewVC animated:YES];
[webViewVC release];
3) In your WebViewViewController, wire up an IBAction method to your "Close" button. In that method use this to dismiss the "pop-over" view.
//
[self dismissModalViewControllerAnimated:YES];
Let me know if you need help with loading the PDF into the UIWebView.

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];