Panel Lookup Web Service (Objective-C, iPhone Example)

A sample implementation of the Panel Lookup Web Service for the iPhone in Objective-C.

- (IBAction)lookupButtonPushed:(id)sender {
	// Setup Request
	NSString *urlString = [NSString stringWithFormat:@"https://ws.valex.com.au/rest/fee-lookup/"];
	NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
	[request setURL:[NSURL URLWithString:urlString]];
	[request setHTTPMethod:@"POST"];
	
	// Setup POST variables
	NSDictionary *postVars = [NSDictionary dictionaryWithObjectsAndKeys:
							  @"myUsername", @"username",
							  @"mySecret", @"password",
							  @"10", @"serv_id",
							  [postcodeTextField text], @"postcode",
							  [suburbTextField text], @"suburb_name",
							  [emvTextField text], @"estimate",
							  nil];
	int postVarCount = 0;
	NSMutableData *postBody = [NSMutableData data];
	for (id postVarKey in postVars) {
		NSString *postVar = [[postVars objectForKey:postVarKey] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
		if (postVarCount) {
			[postBody appendData:[@"&" dataUsingEncoding:NSUTF8StringEncoding]];
		}
		[postBody appendData:[postVarKey dataUsingEncoding:NSUTF8StringEncoding]];
		[postBody appendData:[@"=" dataUsingEncoding:NSUTF8StringEncoding]];
		[postBody appendData:[postVar dataUsingEncoding:NSUTF8StringEncoding]];
		NSLog(@"Post variable: %@=%@", postVarKey, postVar);
		postVarCount++;
	}
	[request setHTTPBody:postBody];
	
	// Get the HTTP response
	NSHTTPURLResponse* urlResponse = nil;  
	NSError *error = [[NSError alloc] init];  
	NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
	NSLog(@"Response Code: %d", [urlResponse statusCode]);
	if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
		
		NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
		NSLog(@"Response: %@", result);
		
		// Process the XML contents of the response
		valfirms = [[NSMutableArray alloc] init];
		NSXMLParser *parser = [[NSXMLParser alloc] initWithData:responseData];
		[parser setDelegate:self];
		[parser parse];
		
		// Display to the user
		NSString *message = [[NSString alloc] init];
		int valfirmCount = 0;
		for (id valfirm in valfirms) {
			if (valfirmCount) {
				message = [message stringByAppendingString:@"\n\n"];
			}
			message = [message stringByAppendingString:valfirm];
			valfirmCount++;
		}
		UIAlertView *alert = [[UIAlertView alloc] init];
		[alert setTitle:@"Valuation Firms"];
		[alert setMessage:message];
		[alert addButtonWithTitle:@"Close"];
		
		[alert show];
		[alert release];
	}
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
	NSLog(@"Found element: %@", elementName);
	if ([elementName isEqualToString:@"valfirm"]) {
		NSString *valfirmName = [attributeDict objectForKey:@"name"];
		if (valfirmName) {
			[valfirms addObject:valfirmName];
		}
	}
}