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 online now   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 online now   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 68020
 
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 online now   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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Learning how to use Xcode / interface builder to program in c/++ furycd001 Mac Programming 6 Sep 20, 2011 04:45 AM
Xcode/Interface Builder Problem netfireapps Mac Applications and Mac App Store 3 Dec 2, 2010 05:46 PM
how to add hyperlink to an image in an e-mail? matt wingfield Mac Applications and Mac App Store 1 Aug 10, 2010 07:20 AM
Cocoa xcode interface builder - Can you change the colors and styles of the window? tjw09003 Mac Programming 3 Feb 12, 2010 08:36 AM
Java in Mac: Interface builder + Code Awareness Amuraivel Mac Programming 2 Jun 21, 2007 10:23 AM


All times are GMT -5. The time now is 01:35 PM.

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

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