Skip to content
jkichline edited this page Aug 25, 2011 · 1 revision

#Why ACHTTP? ACHTTP is an HTTP client abstraction that allows for simple HTTP requests to be made and returned in a workable format. It is designed to allow for less code to be written for simple HTTP requests.

##Architecture While ACHTTP has a client, most of the time you can make the call with class methods on the ACHTTPRequest class directly. The ACHTTPClient is used if you perform a lot of requests and don't want to code settings for each. For instance, you may want to authenticate using a username/password. If you set this on the ACHTTPClient, it will be used for all ACHTTPRequest methods.

##Sample Code Let's dive right in with some sample code. Here we make a request to retrieve the contents of property list file:

[ACHTTPRequest get:@"http://my.domain.com/content/Settings.plist" delegate:self action:@selector(setSettings:)];

That's all there is to it. To do something with the results, we write a method on the delegate like this:

-(void)setSettings:(ACHTTPRequest*)request {
    NSLog(@"Title: %@", [request.result objectForKey:@"title"]);
}

What happens is that the response to our request is sent as a parameter to the method we write. In truth, the parameter value can be either ACHTTPRequest or an NSError if a problem occurred. We may want to handle the error if it happens like this:

-(void)setSettings:(ACHTTPRequest*)request {
    if([request isKindOfClass:[NSError class]]) {
        NSLog(@"Error! %@", request);
    }
    NSLog(@"Title: %@", [request.result objectForKey:@"title"]);
}

You can see that what is returned in the result is an NSDictionary. This is because our PLIST file represents a dictionary of items. Property lists can also represent NSString, NSNumber, NSDate, NSData, and NSArray. ACHTTPRequest will return the corresponding class type in the result property.

##Result Types What if we are not dealing with property lists? ACHTTP handles various return types.

  • PLIST: If a property list XML file is encountered, ACHTTP will return the appropriate Objective-C object type in the result property. This can be NSDictionary, NSArray, NSString, NSNumber, NSDate and NSData.
  • JSON: If a JSON (Javascript Standard Object Notation) resource is used, ACHTTP will parse the response and return it as the appropriate Objective-C datatype. Normally this is NSDictionary or NSArray, although NSString or NSNumber could also be returned.
  • XML: If the response mimetype is either "text/xml" or "application/xml", and not a property list, ACHTTP will parse the results as an NSDictionary representation.
  • Text: If the response is text-based, the result property is set to an NSString of the results.
  • Image: If the response MIME type is an "image", then ACHTTP sets the result property to an UIImage.
  • Data: If none of the above are available, ACHTTP sets the result property to type NSData. This is the same as the receivedData property.

Clone this wiki locally