I'm creating an app which has a webview. When user click to a link or a button in the webview, I want to be able to get the new url, and edit the new url.
shouldStartLoadWithRequest should do the trick, but that method is never called when I click to a link. I cannot find why this doesn't work.
I have read somewhere that I need to add this line :
webView.delegate = (id)self;
I tried it, and still get the same issue. Please help
AppDelegate.m
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *currentURL = [[request URL] absoluteString] ;
NSLog(#"Url: %#", currentURL);
return YES;
}
Controller.m
- (void)viewDidLoad
{
webView.delegate = (id)self;
NSString *tokenString = #"123";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"http://192.168.1.69:8888/webapp/test.php?uid=%#", tokenString];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
If your Controller instance adds itself as the delegate then the method you add to the AppDelegate instance will never be called.
Move the shouldStartLoadWithRequest method into your Controller class.
You've set your view controller as the web view's delegate. But then you've implemented the UIWebView delegate method in our application's delegate instead.
Try implementing that delegate method in your view controller. Your view controller is your web view's delegate.
Related
I am having a splitview application. MasterView is a UITableViewController and the detail view is web view. In the master pane on selecting an entry, another table view (created using one more tableviewcontroller to avoid complexity) appears and detail view shows some page related to the entry. This much is working fine.
Now I want the same with second table view as well i.e. on selecting an entry, the detail view should update accordingly. But its not getting updated. I have made the following function in the first tableViewController class:
-(void) display:(NSString*)theUrl
{
NSLog(#"%#", theUrl);
NSURL *myUrl = [NSURL URLWithString:theUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:myUrl];
splitViewDetailViewController *detailViewController =
self.detailViewController;
detailViewController.webView.scalesPageToFit = YES;
[detailViewController.webView loadRequest:request];
}
I am calling this function from the secondTableViewController and its getting called but detail view isn't getting updated.
In the didSelectRowAtIndex method of secondTableViewController I am doing this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *urlString=#"http://www.google.com";
NSURL *myUrl = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:myUrl];
splitViewSecondViewController *secondDetailViewController = [[splitViewSecondViewController alloc] init];
secondDetailViewController.detailViewController=self.detailViewController;
[self.navigationController pushViewController:secondDetailViewController animated:YES];
self.detailViewController.webView.scalesPageToFit = YES;
[self.detailViewController.webView loadRequest:request];
}
But then the following run time error comes:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString stringByAppendingString:]: nil argument
TableViewController.h
#protocol TableViewControllerDelegate
-(void)display:(NSString*)theUrl :(NSInteger)index;
#end
id<tableViewControllerDelegate>_delegate;
#property(nonatomic,assign) id<tableViewControllerDelegate>_delegate;
TableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndex = indexPath.row;
[self.ResultTbl reloadData];
[_delegate display:(NSString*)theUrl :selectedIndex];
}
masterview(splitview).h
#import "tableViewController.h"
#interface MasterViewController : UIViewController <tableViewControllerDelegate>
masterview(splitview).m
-(void)display:(NSString*)theUrl :(NSInteger)index
{
NSLog(#"%#", theUrl);
NSURL *myUrl = [NSURL URLWithString:theUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:myUrl];
splitViewDetailViewController *detailViewController = self.detailViewController;
detailViewController.webView.scalesPageToFit = YES;
[detailViewController.webView loadRequest:request];
}
I have a table which lists out items I would to make each of the row in the table to point to a URL which is assigned to each item.
In your tableview delegate function didSelectRowAtIndexPath you can use this code to open a url in the Safari application.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSURL *url = [NSURL URLWithString:#"http://stackoverflow.com"];
[[UIApplication sharedApplication] openURL:url];
}
If you don't want to leave your app, you can open the url in a UIWebView.
UIWebView * wv = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,460)];
[self.view addSubview:wv]; // or push onto Navigation Stack
// if adding wv as subview, also need to add a back button to self.view to dismiss webview.
[wv loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString: myURLForSelectedRow]]];
[wv autorelease];
When a cell is selected, the -tableview:didSelectRowAtIndexPath: delegate method will be called. From there, assuming you have the url for each row, the following code will open the URL url in Safari.
NSURL *url = /* Assume this exists */;
if( [[UIApplication sharedApplication] canOpenURL:url] )
{
[[UIApplication sharedApplication] openURL: url];
}
I used the below code to load a webpage. Everything works fine, but I want to stop the
network activity indicator after completing loading the webpage. How can we know that
the webpage is loaded completely.
Anyone please help.
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES; // to stop it, set this to NO
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
The simplest way to do this is to add this line after you instantiate your UIWebView.
[webView setDelegate:self];
Now you will call webViewDidFinishLoad: and the entire method should look like this.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
I'll explain this further since you will absolutely need to understand delegation in the future.
UIWebView is merely an object that displays web data. It doesn't really like handling all this other stuff, because it really just loves web data. Now in your case you wanted to find out when UIWebView was done doing its favorite little task. To do this, your UIWebView gives a shoutout to it's delegate like "Yo holmes, I'm done loading this data now. So go do whatever it is you do now." and your UIWebView continues on its merry way.
So what we did is we told the UIWebView that its delegate, in this case, was our current class by setting the delgate property on webView to self.
From there you can call any of the methods available to the UIWebView delegate (its all in the documentation) and have them perform tasks secondary to the main purpose of the UIWebView.
Make sense?
You just need to set webView.delegate to point to some object, and have that object implement webViewDidFinishLoad:.
To show network Indicator while UIWebView load request use below code..
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
//For Showing NetWork Indicator in Webview
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
Note: UIViewController<UIWebViewDelegate> and [webView setDelegate:self];
here's what I used to stop the activity indicator:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAddress = [NSString stringWithFormat:#"http://www.wikipedia.com/wiki/%#", place.name];
NSURL *url= [NSURL URLWithString: [urlAddress stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[[self webView] setDelegate:self];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicator stopAnimating];
}
- (void) webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
}
So i'm building a webView into my application to show the contents of a URL that I am passing from a selection in a UITableView. I know the UIWebView is loading content properly because if you hard code say http://www.google.ca into the NSURL then it loads fine, however when I'm passing the URL that I parsed from an RSS feed back from the UITableView it won't load the URL properly. I tried the debugger and the URL is coming out as nil right before I try and parse it, however I can use NSLog to print the value of it out to the console.
here's the code in my UIViewController that has my UIWebView
#import <UIKit/UIKit.h>
#interface ReadFeedWebViewController : UIViewController
{
NSString *urlToGet;
IBOutlet UIWebView *webView;
}
#property(nonatomic, retain) IBOutlet UIWebView *webView;
#property(nonatomic, retain) NSString *urlToGet;
#end
Here's the code for my implementation's viewDidLoad method...
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"Url inside Web View Controller - %#", urlToGet);
NSURL *url = [NSURL URLWithString:urlToGet];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
Once again, I can print the URL to NSLog fine and if I hard code the URL into the NSURL object then it will load fine in the UIWebView. Here is where I'm setting the value in my UITableViewController...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ReadFeedWebViewController *extendedView = [[ReadFeedWebViewController alloc] init];
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
extendedView.urlToGet = [[stories objectAtIndex: storyIndex] objectForKey:#"link"];
//NSLog([[stories objectAtIndex: storyIndex] objectForKey:#"summary"]);
NSLog([[stories objectAtIndex: storyIndex] objectForKey:#"link"]);
[self.navigationController pushViewController:extendedView animated:YES];
[extendedView release];
}
However, since I can print the value using NSLog in the extendedView view controller I know it's being passed properly. Cheers
The urlToGet is null into your ReadFeedWebViewController in the viewDidLoad method because when this method is call this variable is not yet affected. The viewDidLoad is call when the initialisation of the ViewController is finished.
I should you to call it in the viewWillAppear method like that:
- (void)viewWillAppear:(BOOL)animated {
NSURL *url = [NSURL URLWithString:urlToGet];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
Also, there is a memory leak in your code. You should release extendedView after pushing it on the navigation controller.
ReadFeedWebViewController *extendedView = [ReadFeedWebViewController new];
if (extendedView != nil) {
... setup extendedView here ...
[self.navigationController pushViewController:extendedView animated:YES];
[extendedView release];
}
Try this
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];
This time, I was wondering when I was supposed to set my properties ...
I got a nav bar which I use to push a new controller (controlling a Web View) :
NewsViewController *webViewController = [[NewsViewController alloc] init]; // I create my controller
webViewController.urlText = #"http://www.google.fr"; // I set the property
InfonulAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.newsNavController pushViewController:webViewController animated:YES];
[webViewController release];
I don't know why but the code below doesn't work :
- (void)viewDidLoad { //viewDidLoad from my webViewController
[super viewDidLoad];
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlText];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
I just want to create a UIWebView but I need to give the controller the URL to use !
Any idea where and when my urlText property needs to be set ?!?
Cheers,
Gauthier
Do you use property correctly? Like
#property(nonatomic,retain) NSString *urlText;
If so, try to use a customized init method like this;
-(id)initWithUrl:(NSString *)url
{
if(self = [super init])
{
self.urlText = url;
}
return self;
}
dont forget to release urlText in dealloc. Now use;
NewsViewController *webViewController = [[NewsViewController alloc] initWithUrl:#"someUrl"];