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];
}