IOS Code Samples

This code sample is for the Get Shop Offerings service.  It covers the everything you need to make the service call but it does not cover iOS service calls in general.   For example, this sample does not cover checking for an internet connection, using a waitView or how you will display your offerings.   Contact Alex @ GiftRocker with questions.

You will need a JSON parser.  SBParser is a good one that should do the trick: http://stig.github.com/json-framework/

Include the following within your .m file:

#import "JSON/JSON.h"

This bit of code will help you create your JSON and make an asynchronous request.  If you have spawned a new view to list this shop’s offerings, this code could be placed in viewWillAppear:

//create Dictionary
NSDictionary *requestDataRaw =
    [NSDictionary dictionaryWithObjectsAndKeys:
       @"<company id>",@"company",
       @"<partner hash>", @"partner",
       @"<partner password>", @"password",
       @"<version of service>", @"version",
       nil];

//turn dictionary into JSON
NSString *sJSON = [requestDataRaw JSONRepresentation];

//format URL
NSURL *url = [NSURL URLWithString:
    [NSString stringWithFormat:@"%@%@",
    @"https://www.giftrocker.com", 
    @"/services/getshopofferings/"]];

//initialize responseData for callback
responseData = [[NSMutableData data] retain];

//prepare request
NSMutableURLRequest *request = 
       [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData =
       [NSData dataWithBytes:[sJSON UTF8String] 
       length:[sJSON length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" 
       forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" 
       forHTTPHeaderField:@"Content-Type"];
[request setValue:
       [NSString stringWithFormat:@"%d", 
       [requestData length]] 
       forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

//make your request
[[NSURLConnection alloc] 
       initWithRequest:request delegate:self];

This section captures the return message coming back from GiftRocker’s server and was taken from the connectionDidFinishLoading method.

//get the response
NSString *responseString = 
   [[NSString alloc] initWithData:responseData 
   encoding:NSUTF8StringEncoding];
	 
[responseData release];

//create a dictionary from the JSON    	
NSDictionary *msg =  [responseString JSONValue];
[responseString release];

if ([[msg valueForKey:@"status"] isEqualToString:@"GOOD"]) {

   //break out payload, offerings and company
   NSDictionary *payload = [msg objectForKey:@"payload"];
   NSArray *myOfferings = [payload objectForKey:@"offerings"];
   NSDictionary *aCompany = [payload objectForKey:@"company"];

   //add name to UILabel message
   message.text = (NSString*)[aCompany valueForKey:@"name"];

   //iterate through offerings appending name to label		
   for (int i = 0; i < [myOfferings count]; i++) {
       NSDictionary *anOffering = [myOfferings objectAtIndex:i]; 
       message.text = 
            [message.text stringByAppendingString:
            [NSString stringWithFormat:@"\n %@", 
            (NSString*)[anOffering valueForKey:@"name"]]];
	}
   }
else if ([[msg valueForKey:@"status"]
                  isEqualToString:@"INFORMATION"]){
	message.text = [msg valueForKey:@"detail"];	
}
else if ([[msg valueForKey:@"status"] isEqualToString:@"ERROR"]){
	UIAlertView *myAlert = 
                     [[UIAlertView alloc] initWithTitle:
                     @"ERROR" message:[msg valueForKey:@"detail"] 
                      delegate:self cancelButtonTitle:@"Ok" 
                      otherButtonTitles:nil];
	[myAlert show];
	[myAlert release];
}
else {
	UIAlertView *myAlert = 
                    [[UIAlertView alloc] initWithTitle:
                     @"ERROR" message:@"Sorry, there was a 
                         problem with this service." 
                      delegate:self cancelButtonTitle:@"Ok" 
                       otherButtonTitles:nil];
	[myAlert show];
	[myAlert release];
}

Comments are closed.