i have a tab bar control. 1st tab contains a navigation control. On the 2nd tab i want to load a web page (say google.com). I have written the code as
NPIViewController.h
#interface NPIViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic,retain) IBOutlet UIWebView *webView;
#end
NPIViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
}
The page just does not load. No compilation or runtime errors. What is wrong in this ?
to know whats wrong you can do this.
- (void)viewDidLoad {
[super viewDidLoad];
webView.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
}
add this new method in your class
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"Error : %#",error);
}
I hope you have connected webView object with its outlet in Interface builder?
Thanks,
You have to use https instead of http. If you use http you will get this error message (use Ravin's code to see the error):
"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."
Create you webView
IBOutlet UIWebView *webView;
Try this code
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];
Restart your IOS simulator.
It is really not evident, but at first check some site in Safari at IOS simulator.
After restart of IOS simulator, my webView opened successfully both at simulator and device.
See this famous link.
Related
I am new in iPhone development and I want to perform a simple task with UIWebView. I just want to display an URL on a new view when i click on a button present in the root view controller. But when i do this i get an exception
NSInvalidArgumentException', reason: 'Application tried to present a
nil modal view controller on target .
Here is my code.
WebViewController.m (This is the view where i have kept the UIWebView)
NSURL *url = [[NSURL alloc] initWithString:#"www.google.com"];
NSURLRequest *requestobj = [NSURLRequest requestWithURL:url];
[webview.delegate self];
[self.view addSubview:webview];
[webview loadRequest:requestobj];
Hope that you have defined the webview by the following. UIWebview is a subclass of UIView.
IBOutlet UIWebView *webView;
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];
Issue
Well the error says you are trying to push a nil value.
Things to check
Make sure the WebViewcontroller is initialised properly[it cannot be nil]
Make sure the nib name is WebViewController
Files owner has class name WebViewController
I have a uiwebview I open http://google.com in it. Then I type something in google search bar and search for something. I want to get that updated url that is generated as a result of my search on google.
- (void)viewDidLoad
{
[super viewDidLoad];
[self setUrlAddress:#"http://google.com.pk"];
// Do any additional setup after loading the view from its nib.
NSURL *url = [NSURL URLWithString:self.urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView setScalesPageToFit:YES];
[self.webView loadRequest:requestObj];
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
[appDelegate showActivityView:self.view];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[appDelegate hideActivityView];
}
Try this
NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:#"window.location.href"];
Hope it will work for you
Below is a correct answer but its not perfectly working for Facebook and youtube
NSSTRing * currentURL = webView.request.URL.absoluteString;
If anyone knows a better answer, please share.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAddress = [NSString stringWithFormat:#"http://www.google.com/pk];
//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];
}
try this delegate method :
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
you can get hold of request and nslog that request .
I am just using this piece of code in viewDidLoad method. It only opens the web page in UIWebView and that's it. But when I go back to the previous view It cause crash.
here is the code:
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[moreWeb loadRequest:requestObj];
Just stop loading like this:
-(void)viewWillDisappear:(BOOL)animated{
[moreWeb stopLoading];
moreWeb.delegate = nil;
}
it'll work fine.
Update: I have recreated the same steps with a new project targeting iPhone and this time it works; I tried again with iPad only project it doesn't! That's weird isn't it ?
Update2: I found why. I saw blank because I put default webview size control and I can see something only if I rotate the iPad simulator :(
====
In xib file I have drag and drop a UIWebView control in my single view app for IPAD Simulator
I drag and drop from the control to file owner to set the delegate
I drag and drop from the file owner to set webView in referencing Outlets
In code I have
// h file
#interface myViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property(nonatomic,retain)IBOutlet UIWebView *webView;
#end
// m file
#import "myViewController.h"
#interface myViewController ()
#end
#implementation myViewController
#synthesize webView;
- (void)viewDidLoad
{
[super viewDidLoad];
// try following complete url
NSString *urlString = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
try that you have connect webview outlet in interfacebuilder and see following code give complete url
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];
Try & check by encoding the string url :
urlString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
may your url is incorrect
try this
NSString *urlAddress = #"http://www.google.com";
I would like to know how do I load a web view from an "- (IBAction) buttonPressed" function.
IBOutlet UIWebView *webView;
-(IBAction)click
{
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];
}
Make the -(IBAction) load a new view, and put a UIWebView in that new view.