How to make Round Rect Button in Nav bar open up homepage in UIWebView - iphone

I'm creating a single view application with a UIWebView with a nav bar on top. In the nav bar, I want to create a button where, when pressed, will bring you back to the homepage of the UIWebView. Currently, when I press the 'home' button on the nav bar, it exits the application and brings me to the URL in Safari. Please help me fix this issue. My code is below:
ViewController.h:
#interface ViewController : UIViewController
{
IBOutlet UIWebView *MyWebView;
}
//Buttons:
-(IBAction)HomeButton;
ViewController.m:
//Buttons:
-(IBAction)HomeButton {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://google.com"]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *myURL = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[MyWebView loadRequest:myRequest];
}

You have to load the URL in your web view like you already do in viewDidLoad instead of calling openURL:.
-(IBAction)HomeButton {
NSURL *myURL = [NSURL URLWithString:#"http://google.com"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[MyWebView loadRequest:myRequest];
}
Btw. it is convention to start your property names with a lower case character. So you might want to rename MyWebView to myWebView.

Related

How can i clear web view on switching from one class to another?

i have a webview in my detailviewcontroller to load html formatted local text to show details, detailviewcontroller also has a back button, that when pressed i want to clear the web view so that i other detail will show it will be clear to avoid a flick of reloading.
in DetailViewController i have-
- (IBAction)backbuttonClicked:(id)sender {
[_delegate backFromDetailView];
}
-(void)clearWebView{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self.detailWebView loadHTMLString:#"<html><head></head><body></body></html>" baseURL:nil];
[self.detailWebView reload];
}
-(void)changeDetailViewTextTo:(NSString *)text{
NSString *htmlString=[HTMLString getHTMLStringForString:text];
[self.detailWebView loadHTMLString:htmlString baseURL:nil];
}
and in mainviewcontroller i have
-(void)backFromDetailView{
((DetailViewController*)_detailsViewController).delegate=nil;
[((DetailViewController*)_detailsViewController) clearWebView];
}
Still flicking the text, please help.
you can load your webview with the default blank page.
[self.detailWebView loadRequest:
[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];

Press a Button and open a URL in another ViewController

I am trying to learn Xcode by making a simple app.
But I been looking on the net for hours (days) and I cant figure it out how I make a button that open a UIWebView in another ViewController :S
first let me show you some code that I have ready:
I have a few Buttons om my main Storyboard that each are title some country codes like UK, CA and DK.
When I press one of those Buttons I have an IBAction like this:
- (IBAction)ButtonPressed:(UIButton *)sender {
// Google button pressed
NSURL* allURLS;
if([sender.titleLabel.text isEqualToString:#"DK"]) {
// Create URL obj
allURLS = [NSURL URLWithString:#"http://google.dk"];
}else if([sender.titleLabel.text isEqualToString:#"US"])
{
allURLS = [NSURL URLWithString:#"http://google.com"];
}else if([sender.titleLabel.text isEqualToString:#"CA"])
{
allURLS = [NSURL URLWithString:#"http://google.ca"];
}
NSURLRequest* req = [NSURLRequest requestWithURL:allURLS];
[myWebView loadRequest:req];
}
How do I make this open UIWebview on my other Viewcontroller named myWebView?
please help a lost man :D
Well, you've firstViewController already designed and coded, now you've to create secondViewController with a UIWebView binded in IB it self, also it have a setter NSString variable like strUrl that you need to pass at the time of pushing or presenting secondViewController, and assign it to UIWebView in viewDidLoad of secondViewController. See my answer about how to pass a NSString? Also UIWebView has its delegates method (What's delegate & datasource methods? - Apple Doc) Which you can use to handle URL request.
These are the delegate of UIWebView, if you'll going to use it, you need to give UIWebView delegate to self.
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
firstViewController.m
- (IBAction)ButtonPressed:(UIButton *)sender {
NSURL* allURLS;
//get your URL
secondViewControlelr *secondView=[[secondViewControlelr alloc]init];
second.urlToLoad=allURLS;
[self.navigationController pushViewController:secondView animated:YES];
}
secondViewControlelr.h
//declare url and set property
NSURL *urlToLoad;
secondViewControlelr.m
- (void)viewDidLoad
{
myWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:myWebView];
NSURLRequest *urlReq=[NSURLRequest requestWithURL:self.urlToLoad cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10];
[myWebView loadRequest:urlReq];
}

reload a UIWebView

I'm using a UIWebView like that in the viewDidLoad:
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 88, 320, 400)]; // init and create the UIWebView
webView.autoresizesSubviews = YES;
webView.userInteractionEnabled = NO;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[webView setDelegate:self];//set the web view delegates for the web view to be itself
NSURLRequest *requestObject = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com"]];//Create a URL object & Set the URL to go to for your UIWebView
[webView loadRequest:requestObject];//load the URL into the web view.
[self.view addSubview:webView];//add the web view to the content view
[webView release], webView = nil;
When I try to change the page, I try that BUT it doesn't working :
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.facebook.com"]]];
[webView reload];
The code that's just above is in the action of a TabBar button.
**EDIT :
When I comment
[webView release], webView = nil;
I can see the apple website but not facebook when I push the button! The screen is refreshing in the apple website and not facebook...**
Hundred of thanks to help ;-)
You've released and set your webView to nil. So the object no longer exists. You are calling loadRequest and reload on a nil object which will do nothing.
Assuming you have webView as an instance variable in your class, remove the line
[webView release], webView = nil;
and instead put that in your dealloc method otherwise when you call
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.facebook.com"]]];
then you will be calling it on a nil object which will do nothing.
Where did you try this code ?
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.facebook.com"]]];
[webView reload];
You already released webview and make it Nil. So it will not work if you did it after it loads www.apple.com.
[webView release], webView = nil;
Remove above two lines once and then try. It should work.
Hope this help.
The potential problem I see is that you are setting the webView value to nil after adding it as subview, but before doing the reload:
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.facebook.com"]]];
[webView reload];
here webView is nil... the object still exists (because it is embedded in a view), but the variable does not point to it anymore, so messages are simply ignored.
no need of reload
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.facebook.com"]]];
}
this may help..!!
Reading your code, I guess that the your webView is declared as #property in the header file. You release this object after the Apple site is loaded. I think that's the reason why it doesn't load the facebook website.
The [webView release]; should be placed within the - (void) dealloc method only.
Please let me know if it works!

How to load a UIWebView with a close button on top?

I currently have the following code that loads a UIWebView from another View. Now is there anyway I can have a close button?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[self.view addSubview:webView];
NSURLRequest *urlRequest;
NSURL *urlforWebView;
urlforWebView=[NSURL URLWithString:#"http://www.google.com"];
urlRequest=[NSURLRequest requestWithURL:urlforWebView];
[webView loadRequest:urlRequest];
}
I am going to load a page built using jquery mobile, so a close button inside the page would also work fine. But on a navigation bar would be ideal. Btw, my application does not have a UINavigationBar
I would create a new sub class of UIViewController, say WebViewController with a nib. Then I would add an UINavigationBar with a close button and an UIWebView. Then to show your web view controller you can do something like:
WebViewController *webViewController = [[WebViewController alloc] init];
webViewController.loadURL = [NSURL URLWithString:#"http://www.google.com"];
[self presentModalViewController:webViewController animated:YES];
[webViewController release];
In your WebViewController you can define:
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#property (nonatomic, retain) NSURL *loadURL;
- (IBAction)close:(id)sender;
and implement something like this:
- (void)viewDidLoad {
[super viewDidLoad]
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.loadURL];
[self.webView loadRequest:urlRequest];
}
- (IBAction)close:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
Whenever you load the UIWebView, you could run a javascript snippet on the content first.
[webView stringByEvaluatingJavaScriptFromString:#"window.close = function() { window.location = 'app://close-webview'; };"];
That just hijacks the normal behavior of window.close() and gives you chance to catch the call in your Objective-C.
Then in your UIWebViewDelegate you could listen for whatever you chose as the close url.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if([request.URL.absoluteString isEqualToString:#"app://close-webview"]) {
webView.hidden = YES;
// or whatever you want to do to remove the UIWebView...
return NO;
}
return YES;
}
A bit hackish, but it would allow the web developer to control the look and feel of the close button from within the HTML content.

Cannot load alternating urls with UIWebview

I have one view holding a list of videos(buttons which can be clicked, each hold a url) which currently bring up a new view which holds the UIWebview, the only problem is I cant pass the url to the webview depending on which button was clicked.
At the moment I just get an empty UIWebview.
How can the url string be passed to the webview so it can load the correct url for each button?
Regards
NSURL *videoURL = [NSURL URLWithString:#"http://..."];
[webView loadRequest:[NSURLRequest requestWithURL:videoURL]];
UPDATE:
In response to comment #3, here's what you can do:
This goes without saying, but keep a reference to the UIWebView instance in Browser
Add an NSString or NSURL #property to Browser
Pass the URL to the Browser instance right after init
In viewDidLoad:, call loadRequest:
So your code might look like this:
- (void)buttonClicked {
Browser *browser = [[Browser alloc] initWithNibName:nil bundle:nil];
browser.URL = [NSURL URLWithString:#"http..."];
[self presentModalViewController:browser animated:YES];
[browser release];
}
and in Browser's viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:URL]];
}