How to Build a Simple Native iOS application

Creating an iOS application is not a tough task, but knowing which libraries are valuable and how to access information easily is definitely worth noting. We recently prepared an internal application that presents all of the important information about Tivix. In essence, this is a kind of business card created for viewing on an iPhone or iPad. When creating this kind of app, the initial question usually is, “How can we obtain accurate information about a company that doesn’t have its own REST service?” The answer is really simple: most companies have Facebook pages, and by using the FB Graph API, this data is available for all. We tried this out for Tivix using this URL:
http://graph.facebook.com/tivix

The Facebook graph API returns a dictionary with all of the basic information about the company, which can then be parsed and presented in any way you desire. Before this is possible, your application must send a request to Facebook, and in turn must process Facebook’s response correctly. There are a few well-known frameworks that simplify this task; currently most developers decide to use AFNetworking, mostly for simplicity, its big community, and easy handling of JSON. The request to Facebook can be written in few lines:

NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/tivix"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

//cast JSON to NSDictionary* and present received data
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

//inform user about problem with connection
}];

https://github.com/AFNetworking/AFNetworking

Since iOS 5.0, the SDK includes native support for JSON. The JSON response can be casted to NSDictionary*.

A Facebook page usually contains a company’s url, telephone number, and location. Here is how we recommend presenting this information to a user:

Home Screen

We used a custom UIButton, the title of which is a link to the Tivix home page. When the user taps it, the proper method uses this title to open the OS web browser:

-(IBAction)showPage:(id)sender
{
UIButton *button = (UIButton *)sender;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:button.titleLabel.text]];
}

Phone Number

We used the same mechanism as above, but to open the phone app, the url format has to be changed:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", button.titleLabel.text]]];

Location

The simplest known method of presenting location is probably the map. In our app we didn’t make an exception. MKMapView is a really easy widget. To present the office location we only had to perform two actions:

1. Add map annotation: we prepared our own class (MapViewAnnotation), which holds information about location and the name of the annotation. Its constructor is:

CLLocationCoordinate2D location;

location.latitude = 37.7828;

location.longitude = -122.4716;

MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Tivix office" andCoordinate:location];

[self.mapView addAnnotation:newAnnotation];

2. Center map exactly over office area: we decided it would be appropriate to show only a small area around the office — say, a quarter square mile. To present the desired area we used following code:

#define METERS_PER_MILE 1609.344

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
[_mapView setRegion:adjustedRegion animated:YES];

That’s all folks! Now you can create your own iOS business card, with basic information about your company.