I want to make a label which indicates if a band is on tour (System of a down fyi)
So I want a label in the interface to adjust to a value given on my server. ( So it
I'd recommend using AFNetworking - http://afnetworking.com/. It will allow you to pull data from your server.
For example, you could create an XML file containing the data that you need. Then you can parse it using NSXMLParser and do whatever you need with the data.
NSURL *feedURL = [[NSURL alloc] initWithString:@"http://yourserver.com/yourxmlfile.xml"];
NSURLRequest *feedRequest = [[NSURLRequest alloc] initWithURL:feedURL];
AFXMLRequestOperation *feedOperation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:feedRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
NSLog(@"XMLRequest successful - starting parser..");
[XMLParser setDelegate:self];
[XMLParser parse];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {
UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[connectionError show];
}];
This is the file you will upload to your server. I use the title yourxmlfile.xml
in the AFXMLRequestOperation
, but call it whatever you want.
<?xml version="1.0"?>
<band>
<bandname>The Band</bandname>
<bandontour>1</bandontour>
</band>
Create an ivar (in your .h) to hold the data as it is parsed.
@property (nonatomic, retain) NSString *currentProperty;
This will temporarily hold elements data, then you need to do something with it when the parser reaches didEndElement
.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"bandontour"]) {
// found the <bandontour> tag
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
self.currentProperty = string;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"bandontour"]) {
// finished getting the data in <bandontour>
// do something now that you've got your data retrieved
if (self.currentProperty) int bandOnTour = self.currentProperty.intValue;
if (bandOnTour == 1) self.yourLabel.text = @"Band is on tour!";
else self.yourLabel.text = @"Not on tour.";
}
}
See NSXMLParser class reference for more information.