displaying web view - iphone

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

Related

Navigating back from UIViewController causes crash

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.

two webPage on single UIWebView

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 the same, when i click on cell of tableView graph is not displaying,
do i need to clear webpage or reload webpage something like that ?
Help Me Guys, Thanx in Advance !!
call this method,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
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];
}
I see from your comment that you use a wrong URL format for the second request:
NSString *urlAddress1 = #”yahoo.com”; //Create a URL object.
NSURL *url1 = [NSURL URLWithString:urlAddress1]; //URL Requst Object
NSURLRequest *requestObj1 = [NSURLRequest requestWithURL:url1]; //Load the request in the
UIWebView. [webView loadRequest:requestObj1];
The URL has to begin with
http://
Otherwise it won't work.

url not loading in UIWebView

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.

Going to a UIWebView from an IBAction

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.

release contents of UIWebView

I have a ViewController and UIWebView created in Interface Builder in it. I fill webView with data like this:
NSString *urlAddress = self.artUrl;
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.webView loadRequest:requestObj];
How should I release content of this webView in didReceiveMemoryWarning?
Thanks
You either release the whole web view (if it is off-screen for example), or nothing at all. If you really need to release the contents, load a blank page.