Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Thor69

macrumors newbie
Original poster
Oct 22, 2009
4
0
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

macrumors newbie
Original poster
Oct 22, 2009
4
0
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

macrumors newbie
Original poster
Oct 22, 2009
4
0
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,
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Read the sticky at the top of this forum and buy a book on iPhone programming. That's how you'll make fast progress.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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?
 

PatrickCocoa

macrumors 6502a
Dec 2, 2008
751
149
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];
}
 

groozz

macrumors newbie
Mar 2, 2010
1
0
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..
:D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.