There are no active ads.

Advertisement

Introduction to iOS Development: Reachability

How do you develop apps for iOS?  It’s a common question that many computer enthusiasts ask, and one we here at TechnoBuffalo hope to answer with this series of posts.

Learning how to develop software can be one of the most intimidating prospects for the average computer user, and with the growing saturation of applications in mobile marketplaces, it is becoming increasingly difficult to get your work noticed. That’s what this series is for, helping you learn iOS development from a conceptual perspective. No prior knowledge of computer programming will be necessary. Over the coming weeks, we’ll examine the iPhone’s ability to deliver immersive, intuitive content, a unique opportunity for both developers and consumers.

Today, we’ll be building examining a few ways that you can notify your user if there is an issue loading a web page. If your application does not include error messages, Apple will reject it on the basis that it will ultimately cause user confusion.

1. First, we must open up Xcode 4, Apple’s latest Integrated Development Environment. Select New Project and follow the on-screen prompts until you are met with the template page. Select View-based Application and make sure that you are targeting the iPhone.

2.  After following Xcode’s instructions on setting up the application, entitle the program Reachability and save the file in a convenient place, preferably one that can be easily remembered and accessed in the future.

3.  First, head over to your initialization file for your view controller (ReachabilityViewController.h). This is where we declare variables that will be defined in our implementation file. We’ll begin by simply creating one IBOutlet.

——————————————————–

#import <UIKit/UIKit.h>

@interface ReachabilityViewController : UIViewController {

IBOutlet UIWebView *technobuffalo; //We’ll be visiting TechnoBuffalo.com in this demo, declares Web View.

}

@end

——————————————————–

4.  Next, head on over to the implementation file for your view controller (ReachabilityViewController.m). Here, you will need to scroll down to the viewDidLoad method and uncomment it by deleting the asterisks and backslashes and the beginning and end of the segment of code. Then, add the following code.

——————————————————–

– (void)viewDidLoad

{

[super viewDidLoad];

[technobuffalo loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@”https://www.technobuffalo.com”]]];

}

——————————————————–

5.  Now it’s time to head on over to your interface file, ReachabilityViewController.xib. Drag a Web View into the pane. Double-click on File’s Owner and move to the Connections pane. Here, connect our “technobuffalo” variable with the Web View. Feel free to build and go at this point to ensure that you succeeded in properly connecting our code with the interface element.

6.  We have to now add code to display an error message when a user’s device cannot connect to a server. Move back to our implementation file and add the following code, an extra method that is based on the condition that the Web View is unable to load.

——————————————————–

-(void)webView:(UIWebView *)technobuffalo didFailLoadWithError:(NSError *)error {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Error” message: @”Web View was unable to load. Please check your Internet connection before relaunching the application.” delegate:self cancelButtonTitle:@”Dismiss” otherButtonTitles: nil];

[alert show];

[alert release];

}

——————————————————–

7.  We have to do one final thing in the interface before running this application’s final build. Move back to ReachabilityViewController.xib. Double-click on File’s Owner and move to the Connections tab. Drag from “New Referencing Outlet” to the Web View and choose “delegate”.

8.  Finally, we can build and run the working application. Check to make sure that your alert is working by turning your computer’s Internet connection off. The alert should display in a matter of seconds and appear like the image below.

Questions?

If you have any questions about the topics discussed in this lesson, feel free to voice them in the comments below. We will do our best to ensure that you have a relatively painless experience developing for iOS. Stay tuned for the next installment of this series, which is released weekly.

To read previous installments from this series, check the links below.

 


Jack McGrath

Rooted in his childhood obsession with dismantling and reassembling gizmos and gadgets around the house, Jack McGrath's knowledge of programming,...

Advertisement