Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 23, 2009, 02:47 PM   #1
Thor69
macrumors newbie
 
Join Date: Oct 2009
Hyperlinking a button to an URL in XCode/Interface Builder?

I'm a novice in iPhone app programming; just bought my kit last week and pretty much learning on the fly and slowly making progress.


Unfortunately, I've hit a brick wall in regards to the following issue :
I'm working on what should be a simple app : I'm creating a "portal" app which will allow a user to click on a button in the app which will forward them to a specific URL (website). How do I get the button to link to a specific website which tapped by the end-user?

I've been researching this in vain for the past 2 days; both on Apple's site as well as Google but no luck thus far.

Any assistance would be greatly appreciated!
Thanks in advance.
Thor69 is offline   0 Reply With Quote
Old Oct 23, 2009, 02:57 PM   #2
icewing
macrumors 6502
 
Join Date: Jun 2008
Location: St Louis
Is the URL going to be hard-coded, or will the user be able to change it?
__________________
2012 15.4" MBP 9,1, 2.6 GHz Intel Core i7, 8 G Ram, High-Res Anti-Glare
icewing is offline   0 Reply With Quote
Old Oct 23, 2009, 03:37 PM   #3
Thor69
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
Hi thanks for the reply,
The URL will be hardcoded.
Example : Click on the button and it takes you to www.google.com

Thanks again
Thor69 is offline   0 Reply With Quote
Old Oct 23, 2009, 05:41 PM   #4
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
What you want to do is "link" the button to an action. And then have that action take you to the website.
__________________
dejo is offline   0 Reply With Quote
Old Oct 23, 2009, 06:11 PM   #5
Thor69
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
Quote:
Originally Posted by dejo View Post
What you want to do is "link" the button to an action. And then have that action take you to the website.
Of course; but if I knew how to do that, then I wouldn't be here asking
Thor69 is offline   0 Reply With Quote
Old Oct 23, 2009, 06:28 PM   #6
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by Thor69 View Post
Of course; but if I knew how to do that, then I wouldn't be here asking
Well, which part are you unsure about? The first one? If so, check out Your First iPhone Application guide from Apple. That should teach you how to hook up an action to a button. If it's the second one, check out this thread: iPhone SDK - create a button with URL to open Safari
__________________
dejo is offline   0 Reply With Quote
Old Oct 24, 2009, 11:45 AM   #7
Thor69
Thread Starter
macrumors newbie
 
Join Date: Oct 2009
Quote:
Originally Posted by dejo View Post
Well, which part are you unsure about? The first one? If so, check out Your First iPhone Application guide from Apple. That should teach you how to hook up an action to a button. If it's the second one, check out this thread: iPhone SDK - create a button with URL to open Safari
Thanks for the feedback; much appreciated.

I'm pretty sure that I've figured out how to hook up an action to a button in Interface Builder.

You'll have to forgive my ignorance, but where I'm really having a tough time is figuring out which .h and .m files need further coding configuration as mentioned in the second link which you provided

Thanks again,
Thor69 is offline   0 Reply With Quote
Old Oct 24, 2009, 03:20 PM   #8
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
Read the sticky at the top of this forum and buy a book on iPhone programming. That's how you'll make fast progress.
PhoneyDeveloper is offline   0 Reply With Quote
Old Oct 24, 2009, 04:59 PM   #9
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by Thor69 View Post
You'll have to forgive my ignorance, but where I'm really having a tough time is figuring out which .h and .m files need further coding configuration as mentioned in the second link which you provided
Which file is your action from the first part defined in?
__________________
dejo is offline   0 Reply With Quote
Old Oct 24, 2009, 10:15 PM   #10
PatrickCocoa
macrumors 6502a
 
Join Date: Dec 2008
Here's the answer:

1. In Xcode, select File / New Project / iPhone OS / Application / Utility Application.

2. In Interface Builder, create a button and link it to a method in the MainViewController, let's call the method "showTheSite". Add the following method to MainViewController:

Code:
-(IBAction) showTheSite:(id) sender 
{
	
	FlipsideViewController *controller = [[FlipsideViewController alloc] 
initWithNibName:@"FlipsideView"
bundle:nil];
	controller.delegate = self;
	controller.sentURL = @"http://www.smwspotlighters.com";
//	controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
	[self presentModalViewController:controller animated:YES];
	
	[controller release];
3. Now in FlipSideViewController, you need to actually call the site and show it. In the viewDidLoad method (which Cocoa calls for you once your new Flip Side shows up), you send a message to your new "loadURL" method. The loadURL does the work (to show a website, you need to create a NSURL object and an NSURLRequest, and then send the request to the webview. Opps, I forgot to tell you to create the webview on the FlipSide in Interface Builder earlier. Also I didn't mention that you need to have set up an instance variable that I called "sentURL" in FlipSideViewController.

Code:
- (void)viewDidLoad 
{
	[super viewDidLoad];
	self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; 
	[self loadURL];
}

- (IBAction)done {
	[self.delegate flipsideViewControllerDidFinish:self];	
}

 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
 // Return YES for supported orientations
 // return (interfaceOrientation == UIInterfaceOrientationPortrait);
	return YES;
 }

-(void) loadURL
{ 
	NSURL *theURL = [[NSURL alloc] initWithString:sentURL]; 
	NSURLRequest *request = [[NSURLRequest alloc] initWithURL: theURL]; 
	[mainWebView loadRequest: request]; 
	[request release];
	[theURL release];
}
4. So now you're done! Assuming you've set up all of the parts I didn't go over, everything should work. I didn't go over the .h files for either MainViewController or FlipSideViewController.

5. But not so fast! If you want your app to pass Apple's review process to get into the App Store, you must gracefully handle the connection to the internet and possible failures. There are three methods that Cocoa calls for you when you sent the message earlier to webView. Add them to FlipSideViewController.

Code:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
//	NSLog(@"FlipsideViewController/webViewDidStartLoad");
	// starting the load, show the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//	NSLog(@"FlipsideViewController/webViewDidFinishLoad");
	// finished loading, hide the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}


- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
	// The three internet routines (bought to you courtesy of <UIWebViewDelegate>) are:
	// webViewDidStartLoad, webViewDidFinishLoad, and didFailLoadWithError. See the
	// Xcode project UICatalog.
	
//	NSLog(@"FlipsideViewController/webView didFailLoadWithError");
	// load error, hide the activity indicator in the status bar
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	
	// report the error inside the webview. error.LocalizedDescription has the error, but I don't use it
	NSString* noInternetString = [NSString stringWithFormat:
	@"<html><center><font size=+5 color='red'>Sorry!<br>Internet connection unavailable.<br>Please try again later.</font></center></html>"];
	[mainWebView loadHTMLString:noInternetString baseURL:nil];
}
PatrickCocoa is offline   0 Reply With Quote
Old Mar 3, 2010, 01:09 AM   #11
groozz
macrumors newbie
 
Join Date: Mar 2010
link to URL

Opps, I forgot to tell you to create the webview on the FlipSide in Interface Builder earlier. Also I didn't mention that you need to have set up an instance variable that I called "sentURL" in FlipSideViewController.
.
can u explain how to create what u forgot to explain.. all of us new in this..
groozz is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Interface Builder WTF. SolarShane Mac Programming 2 Mar 12, 2014 02:38 AM
Pashua gui tool interface elements anything similar in xcode interface builder wild4life2013 Mac Programming 1 Aug 10, 2013 08:13 PM
OpenGL ES without interface builder? blueshogun96 iPhone/iPad Programming 1 Feb 10, 2013 01:04 PM
Im having a bit of an issue with interface builder adildacoolset iPhone/iPad Programming 5 Oct 12, 2012 12:51 PM
HyperLinking Buttons in Xcode? DarthVader1213 Mac Programming 1 Aug 15, 2012 09:21 AM

Forum Jump

All times are GMT -5. The time now is 08:44 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC