Since a lot of API responses are in XML, we'll review XML parsing in iOS to help you get to the interesting data faster.
This tutorial assumes that you've already gone through our iOS getting started tutorial and are familiar with how our iOS SDK works.
1 Log in to Temboo and go to the Yahoo > Weather > GetWeatherByAddress Choreo in our Library.
2 Enter a location in the Address input and click Generate Code to test the Choreo from our website.
3 You get a whole bunch of XML in the Response output with lots of weather-related information about the location. Next we'll see how to parse through this response in iOS and pick out only the pieces we're interested in.
4 Create a new OS X > Application > Command Line Tool project in Xcode. Make sure to add the core
directory from the Temboo iOS SDK and the TMBYahoo.h
and TMBYahoo.m
files as described in our getting started tutorial.
5
Copy the code below into your main.m
class, between the Foundation.h
import statement and the main
method. Steps 7 through 9 talk you through what happens in the code.
#import "TMBYahoo.h" #import "TMBChoreography.h" #import "TMBTembooSession.h" @interface GetWeatherByAddress : NSObject <TMBChoreographyDelegate, NSXMLParserDelegate> -(void)runGetWeatherByAddressChoreo; -(void)choreographyDidFailWithError:(NSError*)error; -(void)choreographyDidFinishExecuting:(TMBYahoo_Weather_GetWeatherByAddress_ResultSet*)result; @end @implementation GetWeatherByAddress -(void)runGetWeatherByAddressChoreo { // Instantiate the Choreo, using a previously instantiated TembooSession object, eg: TMBTembooSession *session = [[TMBTembooSession alloc] initWithAccount:@"ACCOUNT_NAME" appKeyName:@"APP_NAME" andAppKeyValue:@"APP_KEY"]; // Create the choreo object using your Temboo session TMBYahoo_Weather_GetWeatherByAddress *getWeatherByAddressChoreo = [[TMBYahoo_Weather_GetWeatherByAddress alloc] initWithSession:session]; // Get Inputs object for the choreo TMBYahoo_Weather_GetWeatherByAddress_Inputs *getWeatherByAddressInputs = [getWeatherByAddressChoreo newInputSet]; // Set inputs [getWeatherByAddressInputs setAddress:@"104 Franklin St, New York, NY"]; // Execute choreo specifying this class as the choreo delegate [getWeatherByAddressChoreo executeWithInputs:getWeatherByAddressInputs delegate:self]; } // TMBChoreographyDelegate method implementation - handle choreo errors -(void)choreographyDidFailWithError:(NSError*)error { // Log error to the console NSLog(@"Error - %@", error); } // TMBChoreographyDelegate method implementation - choreo executed successfully -(void)choreographyDidFinishExecuting:(TMBYahoo_Weather_GetWeatherByAddress_ResultSet*)result { // Log results to the console NSString *response = [result getResponse]; NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[response dataUsingEncoding:NSUTF8StringEncoding]]; [parser setDelegate:self]; [parser parse]; } // NSXMLParserDelegate method implementation - an element was found -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if([elementName isEqualToString:@"yweather:condition"]){ // Found our element, print out weather description and temperature NSLog(@"%@, %@ degrees", [attributeDict valueForKey:@"text"], [attributeDict valueForKey:@"temp"]); } } @end
6 Update your main
method to run the Choreo:
int main(int argc, const char * argv[]) { @autoreleasepool { GetWeatherByAddress *test = [[GetWeatherByAddress alloc] init]; [test runGetWeatherByAddressChoreo]; [[NSRunLoop currentRunLoop] run]; } return 0; }
7Now let's see what's going on in the code. First we create a NSXMLParser
and specify our class as the delegate. To do this our class is required to conform to the NSXMLParserDelegate
protocol.
8By implementing the parser:didStartElement:namespaceURI:qualifiedName:attributes
method of that protocol, we will be notified (the method will be called) for each new element the parser encounters. All that's required is checking the element name to see if it matches the element we're interested in, in this case the yweather:condition
element.
9Once we find the element we're looking for, we simply request the attribute values from the attributeDict
dictionary object, by name.
10That's it! You can run the code in Xcode to try it out. You should see the weather condition text and temp printed in the console.
Now you're ready to tackle all sorts of XML parsing tasks. Check out the 2000+ Choreos in our Library and find some exciting data to parse.
Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.
We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.
Like what we do? Take a look at our open positions.