iOS Load Web Page in UIWebView from Another View Controller - iphone

I have a tabbed application, with a tab that contains a UIWebView and a second tab with a Table View Controller.
I'm trying to make it so that when I click on a row in the table it takes me to my other tab and loads a web page.
Here's my storyboard:
This is in my implementation for the web view:
-(void)loadAViewer:(NSURL*)viewerURL
{
NSURLRequest *requestObj = [NSURLRequest requestWithURL:viewerURL];
[sfnViewer loadRequest:requestObj];
}
And then in the table view controller:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the URL from plist
...
//Generate url, pass it in and switch to web view tab
viewerURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#/viewer?username=%#&pw=%#&%#", web, loc, user, pass, params]];
ViewerFirstViewController *viewerWebView = [[ViewerFirstViewController alloc] init];
[viewerWebView loadAViewer:viewerURL];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tabBarController setSelectedIndex:0];
}
Currently nothing happens. I'm taken to the other tab but with a blank UIWebView - no page loads but it doesn't crash either.
//Edit - Changes made based on answer from Steve
Based on this I've added
#property (strong, nonatomic) NSURL viewerUrl;
in my .h file for the UIWebView tab.
I've synthesized it in the .m and added this to my viewDidLoad method.
NSURLRequest *requestObj = [NSURLRequest requestWithURL:viewerURL];
[sfnViewer loadRequest:requestObj];
In the Table View Controller I'm now doing this.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the URL from plist
...
//Generate url, pass it in and switch to web view tab
viewerURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#/viewer?username=%#&pw=%#&%#", web, loc, user, pass, params]];
ViewerFirstViewController *viewerWebView = [[ViewerFirstViewController alloc] init];
[viewerWebView setViewerUrl:viewerURL];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.tabBarController setSelectedIndex:0];
}
Still to no avail.

It's probably not loading because when you call loadAViewer: the view itself hasn't been loaded yet so the sfnViewer outlet won't be hooked up to the UIWebView in your storyboard.
You can confirm this by checking if sfnViewer is nil in loadAViewer:.
If this is the case you will have to define an NSURL property on the ViewerFirstViewController and assign to that instead of calling loadAViewer:. Then in the viewWillAppear: method you should load the URL into sfnViewer.
However this will result in the web view being reloaded each time the tab is accessed, you could avoid this by checking if viewerURL is different from the URL currently in use in the web view: [sfnViewer.request.URL isEqual:self.viewerURL], if it is the same then don't create/load the request.

Related

iOS Storing and Loading Multiple web views in and from an array

My app used to use a single UIWebView from the storyboard to display a users online data.
Now I have added the ability to have multiple accounts stored in the app, and so I would like to be able to have a separate UIWebView for each account, and show the appropriate one.
My app is a tabbed application, with one tab holding the account details in a table view, and the other with the UIWebView. This is what I have tried so far to have multiple web views.
In the table view controller, my didSelectRowAtIndexPath looks like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Generate the url (please note I've taken this code out)
...
//Get the view controller that will hold the web view
NSArray *viewControllers = self.tabBarController.viewControllers;
ViewerFirstViewController *viewerViewController = [viewControllers objectAtIndex:0];
//Look for the web view in an array of web views
[viewerViewController getWebView:indexPath.row];
[viewerViewController setViewerUrl:viewerURL];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//Move to the tab that will hold the web view (bonus question - how can I animate this move?
[self.tabBarController setSelectedIndex:0];
}
Now, for the view controller that will hold the web view
-(void)getWebView:(int)index
{
if ([webViews objectAtIndex:index] == NULL) {
[self createWebView:index];
[self setDidExist:false];
[self setWebView:[webViews objectAtIndex:index]];
} else {
[self setDidExist:true];
[self setWebView:[webViews objectAtIndex:index]];
}
}
-(void)createWebView(int)index
{
float width = self.view.bounds.size.width;
float height = self.view.bounds.size.height;
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, width, height)];
wv.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
wv.scalesPageToFit = YES;
wv.delegate = self;
[self.view addSubview:wv];
[webViews insertObject:wv atIndex:index];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewDidLoad];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:viewerUrl];
if (didExist) {
[webView loadRequest:requestObj];
[webView setHidden:false];
} else {
[webView loadRequest:requestObj];
}
}
It appears that the web view is being created as I am presented with a white screen that I can scroll in, but no page is being loaded. I'm assuming this won't work to hide the web views not currently in use either.
The web view should only load the page if it didn't exist previously. If it did already exist then it should present the loaded web view that is stored in the array.
Am I loading the URL at the wrong time or something? I'm unsure as to why I get a web view created but it's not filled with anything..
What seems to be happening is the web view isn't being added to the array when I create it - the array is empty and so webView isn't being set and so the url is never loaded into it However the code I use to add it to the array seems ok to me..
[webViews insertObject:wv atIndex:index];
I changed it from storing them in an NSMutableArray to an NSMutable Dictionary. This solved my problems.

Unable to load WebView from TableView in PopOverController - Objective C

I have an app. In this on the click of an Upload button a UITableView appears inside a UIPopOverController with a few entries. On clicking on any of these entries, a corresponding site should open in the wUIWebView of my UIDetailView of SplitView app.
I am using the following lines of code ::
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
hUrl = [kk objectAtIndex:indexPath.row];
hUrl = [Url stringByReplacingOccurrencesOfString:#" " withString:#"_"];
NSString *hisPage= [[NSUserDefaults standardUserDefaults] stringForKey:#"url_preference"];
Page = [hisPage stringByAppendingString:#"/index.php/"];
Page = [hisPage stringByAppendingString:hUrl];
NSURL *Url = [NSURL URLWithString:Page];
NSLog(#"%#", Url);
NSURLRequest *request = [NSURLRequest requestWithURL:Url];
self.detailViewController.webView.scalesPageToFit = YES;
[self.detailViewController.webView loadRequest:request];
}
The UITableView appears inside a UIPopoverController. However, I am unable to load the corresponding webPage. Can someone help me to sort this out ?? Thanks and regards.
Per your answer to my comment, the proper procedure to follow in this scenario is as follow:
Implement a delegate method protocol for your popOverController instant.
In didSelectRowAtIndexPath, call the delegate method back to the parent view controller (where your Upload button located). And passing back the Url string as a parameter.
From the parent view controller, dismiss the popover and using the passed back Url to prepare your detailViewController and then push it onto display stack.
An example of delegate method can be found in this SO .

on click of UITableViewCell, UIWebView not able to Load webPage

i am making an iPad application,
i want to load 2 webPage on single UIWebView one after another,
1st webpage should come when i load my application,and 2nd webpage should come on click of cell of tableView,
so,inside my didLoad method i am writing this (1st webpage),
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];
it works fine..
but on the same page on same webview on click of cell of tableView i want to load another page,(2nd webpage)
i written code for loading 2nd webPage,
here is the code snippet for 2nd webPage,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DatabasesViewController *nextController=[[DatabasesViewController alloc]initWithNibName:#"DatabasesViewController" bundle:nil];
[self.navigationController pushViewController:nextController animated:YES];
[nextController changeProductText:[a objectAtIndex:indexPath.row]];
[nextController changeProductText1:[b objectAtIndex:indexPath.row]];
[nextController changeProductText2:[d objectAtIndex:indexPath.row]];
}
when i selects a cell inside tableView,
it navigates to new view, i don't want to navigate, i want to stay on same page what changes should i do in above function ?
in above function changeProductText,changeProductText1,changeProductText2, are three IBAction and a,b,d are my array names.
- (IBAction) changeProductText:(NSString *)str{
l1=#"http://ipad.appel.com/indexcharts.aspx?id=";
l2=str;
NSString *str1 = [l1 stringByAppendingString:l2];
NSURL *url2=[NSURL URLWithString:str1];
NSURLRequest *req2=[NSURLRequest requestWithURL:url2];
[webView loadRequest:req2];
}
when i click on cell of tableView webView is not displaying,
do i need to clear webpage or reload webpage something like that ?
Help Me Guys, Thanx in Advance !!
I think You can use the UISplitViewController for this issue.
EDIT : Now check out the code like this..
-(void)tableView:(UITableView *)tableView didSelectedRowAtIndexPath:
(NSIndexPath *)indexPath
{
NSString *urlString = [siteAddress objectAtIndex : indexPath.row];
detailViewController.webView.scalesPageToFit= yes;
NSUrl *url = [NSUrlRequest requestWithURL : url];
[detailViewController.webView loadRequest : request];
}
[self.navigationController pushViewController:nextController animated:YES];
This navigates you away from the page. It pushes a new view onto the navigation stack. Get rid of it if you don't want to do that.What is this a splitView?

How to load web page using didSelectRowAtIndexPath without leaving the app?

I am parsing RSS feeds and loading results into a table. When the user clicks on a story, they are taken to the web page.
At the moment I am using:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:story]];
Trouble is that this launches Safari and closes my app.
Is there a way to open the webpage in my app so to speak? I'd like the webpage to appear with a "Done" button that closes the web page and returns the user back to the table.
Any help (and code examples) are much appreciated.
You can use UIWebView in a new view controller launched via navigationController pushViewController selector, and have a toolbar item to "View in Safari" which will launch full Safari browser (and putting your app in the background/terminating).
Some example code snippets:
Assumptions:
(1) You have a view controller entitled: WebViewController (with same .xib) file.
(2) This view controller has a UIView which is subview of the main view;
#interface WebViewController: UIViewController <UIWebViewDelegate> {
UIWebView *webview;
NSString *someUrlToLoad;
}
#property (nonatomic, retain) IBOutlet UIWebView *webview;
#property (nonatomic, copy) NSString *someUrlToLoad;
In this view controller's viewDidLoad method:
-(void) viewDidLoad {
[super viewDidLoad];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.someUrlToLoad]]];
}
You load this controller when someone clicked on the cell in your tableview:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WebViewController *viewController = [[WebViewController alloc] initWithNibName:#"WebViewController" bundle:nil];
viewController.someUrlToLoad = <url for this cell>;
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
}
Most likely you will want to include some hook into the UIWebViewDelegate to stop/show an activity indicator in the navigation bar so the user is aware that something is happening while the page loads.

"prefetch" HTML for UIWebView and pass it to a new controller

I have a navigation-based application with 2 UIViewControllers (controller-2 is pushed onto controller-1 when the user selects a particular row within a UITableView displayed using controller-1).
Here's the 'twist'… When controller-1 is loaded, I want to "prefetch" a URL containing HTML/javascript into a UIWebView. Initially, the user won't see this UIWebView. Instead they will see a UITableView allowing them to click on 5 different options (row0, row1, row2, row3, row4).
When the user selects a row I would like to push a new view onto the stack and "pass" the HTML/Javascript to a UIWebView located within this new view, and display the web-page in the new view.
Is it possible to pass the UIWebView like this?
I've tried the following, which is not displaying the HTML within UIWebView, so any suggestions on how to pass the UIWebView (or alternatives) would be great. I'm pretty sure I've made the correct connections within my nib file for controller2.
To explain my motives. I want to use javascript to hide certain elements of the web-page depending on which row the user clicks. To me, it's faster and easier to make 1 URL request and then use javascript to hide various HTML elements than it is to make 5 different URL requests, fetching 5 separate web-pages. So, if there are alternative thoughts to this approach, that would be great, too. I thought about doing this all within 1 viewcontroller (adding and removing subviews), but I'd rather push onto a new view to take advantage of the navigation controller.
TYVM!!!!
////////// controller 1 //////////////////////
- (void)viewWillAppear:(BOOL)animated {
// do other stuff ///
NSURLRequest *request_object = [NSURLRequest requestWithURL:self.url];
[self.web_view1 loadRequest:request_object];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.view_controller2 = [[[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil] autorelease];
self.view_controller2.web_view2 = self.web_view1;
[self.navigationController pushViewController:self.view_controller2 animated:YES];
}
//////////// controller 1 //////////////////////
//
///////////// portion of controller 2 header file /////////////////////////
#interface ViewController2 : UIViewController {
IBOutlet UIWebView *web_view2;
}
#property (nonatomic, retain) IBOutlet UIWebView *web_view2;
//////////// portion of controller 2 implementation file //////////////
#import "ViewController2.h"
#implementation ViewController2
#synthesize web_view2;
- (void)dealloc {
[web_view2 release], web_view2 = nil;
[super dealloc];
}
//////////// controller 2 //////////////////////////
You've got the right idea, but you need to do one other thing. In your -tableView:didSelectRowAtIndexPath:. you need to add the webview to the viewController's view, not just assign it to an outlet. The following might work, depending on your XIB files:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.view_controller2 = [[[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil] autorelease];
self.web_view1.frame = self.view_controller2.web_view2.frame;
[self.view_controller2.web_view2 removeFromSuperview];
self.view_controller2.web_view2 = self.web_view1;
[self.view_controller2.view addSubview: self.web_view1];
[self.navigationController pushViewController:self.view_controller2 animated:YES];
}
This assumes that web_view2 is a valid and hooked up outlet, containing a webview in the location you want your 'cached' view to be.