Hostingireland App on the App store and covered by silicon republic

 Development, Ios  Comments Off on Hostingireland App on the App store and covered by silicon republic
Oct 232011
 

I have been beavering away in hostingireland.ie on a native iOS app for our clients to use for managing their accounts, ordering new domains, sending support tickets and so on. And now its live an up on the app store and has been covered by silicon republic read it here and will also quite possibly be at least mentioned in the Sunday Business Post. I hope that in the future, we will see lots of people using this app and also that we can add and improve on its features.

I was quite excited about this app as it gave me the opportunity to build something that I knew would be useful and also that would definitely end up on the App Store. With a pre built audience, I also knew it would see use.
As my friends know, I have built several apps for iOS but have decided for one reason or another not to publish them to the app store, well at least not yet anyway. Seeing this app on the App Store has given me a lot of confidence to complete some of my own projects and get them up on the store too. Watch this space :)

Now for some boring programming stuff

For any interested, I am going to run through some of my thoughts and decisions made while creating this app.
Web Api

The first steps to building the app, after planning out its features, was to build the web api.I used PHP for this as this is the language used in the company for most programming tasks.

This app required strong security and also easy extensibility. The app makes each request via https and uses two layers of authentication and validation. Easy extensibility was achieved by creating a set of abstract class which took care of a lot of the common business logic but left the variables and base implementation up to its concrete children by forcing the override of several abstract methods. The abstract api method controller also implements a method controller interface. This means I can easily extend the Api to add new features/methods in the future.

The App

Often when building apps with lots of options and data, you feel the need to have UITabBarController as well as a UINavigationController. This isn’t a template in Xcode but can be easily achieved by creating a RootViewController that extends UINavigationController for each tab option and then adding this controllers as the UITabBars controllers. Maybe an example would be good.


RootViewController * accountView = [[[RootViewController alloc]initWithNibName:nil bundle:nil] autorelease];
accountView.tabBarItem.title = @"Account";
accountView.tabBarItem.tag = TAB_CONTROLLER_ACCOUNT;
accountView.tabBarItem.image = [UIImage imageNamed:@"111-user.png"];

//Add as tabbar view controllers

NSArray * controllers = [NSArray arrayWithObjects:accountView,domainRegView,supportView,productsView,moreView,nil];
self.tabBarController.viewControllers = controllers;


In the root view controller you then have a switch which would look at the tag of the tabbaritem and push on to the stack the appropriate UIViewController.

Other difficulties I faced in the app itself included allowing the app to cache certain api call responses and deciding how and when an api call response should be refreshed. To achieve this I created a cache manager which exposed several methods to allow a cache to be checked, set to refresh or a new cache to be created. The aim of the cache was to reduce the potential number of requests being sent to the web server and to reduce any latency in the app wherever possible.

Another important factor was allowing multiple web requests to be made from the app simultaneously and without locking up the UI. To do this I created an extension of NSOperation which performed the calls on background threads rather than the main UI thread. I also created a protocol with methods it would call on a delegate based on the type of response sent back by the api. To allow easy use, I created an api method factory class which which was a singleton and had a handle to a shared NSOperationQue.

Overall it was a great experience building this app

Objective C ASynchronous Http Request Wrapper

 Development, Ios  Comments Off on Objective C ASynchronous Http Request Wrapper
Oct 232011
 

So having worked on two apps now that required many requests to web servers to be made in the background, I decided to write a simple to use wrapper around the more complex parts which exposes what I hope is a clear api.

The basic concept is an extension to NSOperation overriding the methods:

- (void)start;
- (BOOL)isConcurrent;
- (BOOL)isFinished;
- (BOOL)isExecuting;

I aslo made some simple Http Request and response wrappers and a protocol for receiving the responses called HttpResponseDelegate


@protocol HttpResponseDelegate

@required
- (void)requestResponseRecieved:(HttpResponse *)res;

@optional
- (void)requestResponseError:(HttpResponse *)res;
- (void)requestFailed:(NSError *)err;
- (void)request:(HttpRequest *)req AndOpCancelled:(HttpOperation *)op;
@end

The wrapper will let you send both post and Get requests which are cancelable and will be performed on background threads; although it doesn’t yet allow for the posting of multipart data such as images, but I plan to add that in time. I know there are other libraries out there for this kind of thing, but I wanted to do it for myself as a personal experiment and project. I have put the code up on git hub. Feel free to use it as you wish Git Hub Repo

Basic Usage

Just to give a quick usage example:

in a viewController implementing the HttpResponseDelegate protocol you would do something along the lines of :

- (void)someMethod{
//this is a singleton class that manages adding ops to a single operation que
HttpSync * httpFactory = [HttpSync getSharedInstance];
HttpRequest * req = [[HttpRequest alloc]initWithUrl:@"http://apple.com"];
HttpRequest * req2 = [[HttpRequest alloc]initWithUrl:@"http://google.com"];

//send some params
[req2 setParamWithKey:@"someparam" AndValue:@"somevalue"];

//change to POST req.method = HTTP_METHOD_POST;
//default method is GET

req.method = HTTP_METHOD_GET;
//make the operations possibly could add this functionality to the HttpSync class in future
HttpOperation * httpOp = [[HttpOperation alloc] initWithRequest:req AndResponseDelegate:self];
HttpOperation * httpOp2 = [[HttpOperation alloc] initWithRequest:req2 AndResponseDelegate:self];
[httpFactory sendOperation:httpOp];
[httpFactory sendOperation:httpOp2];
[req2 release];
[req release];
[httpOp release];
[httpOp2 release];

}

#pragma mark HttpResponseDelegate methods

- (void)requestResponseRecieved:(HttpResponse *)res{
if(res.responseCode == HTTP_RESPONSE_CODE_OK){
//do your thing get the content by using
NSData * resData = res.rawResponse;
NSString * resString = res.responseAsUTF8DecodedString;
//see the headers
NSDictionary * headers = res.responseHeaders;

}else{
//check the response code and respond accordingly
}
}

Gah objective c does not come out well on the blog. Hopefully you can follow it. Will be adding some improvements as I hope to use this library in some of my future projects.

© 2012 Craig Brookes Suffusion theme by Sayontan Sinha