How do I link live currency exchange rates to my iPhone app? First, anyone know any sites where I can get the exchange rates? And second, how do I link that to my app? I want to do what this app does. http://the-dream.co.uk/currencee/
问题:
回答1:
Here is a blog post about this, however to recap, if you use TBXML you can do it with the methods below.
They do the following:
- Assume you have made a mutable dictionary object as a class property called exchangeRates
- Set's EUR as the base rate (value of 1.0)
- Call the European Central Bank's exchange rate XML feed and parses it.
After you've called the loadExchangeRates() method you can obtain a specific exchange rate by doing:
NSDecimalNumber *rate = [NSDecimalNumber decimalNumberWithString:[self.exchangeRates objectForKey:@"USD"]];
Here are the methods:
- (void)loadExchangeRates { // initialize rate array exchangeRates = [[NSMutableDictionary alloc] init]; // Load and parse the rates.xml file TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"]] retain]; // If TBXML found a root node, process element and iterate all children if (tbxml.rootXMLElement) [self traverseElement:tbxml.rootXMLElement]; // add EUR to rate table [exchangeRates setObject:@"1.0" forKey:@"EUR"]; // release resources [tbxml release]; } - (void) traverseElement:(TBXMLElement *)element { do { // Display the name of the element //NSLog(@"%@",[TBXML elementName:element]); // Obtain first attribute from element TBXMLAttribute * attribute = element->firstAttribute; // if attribute is valid NSString *currencyName; while (attribute) { /* Display name and value of attribute to the log window NSLog(@"%@->%@ = %@", [TBXML elementName:element], [TBXML attributeName:attribute], [TBXML attributeValue:attribute]); */ // store currency if ([[TBXML attributeName:attribute] isEqualToString: @"currency"]) { currencyName = [TBXML attributeValue:attribute]; }else if ([[TBXML attributeName:attribute] isEqualToString: @"rate"]) { // store currency and rate in dictionary [exchangeRates setObject:[TBXML attributeValue:attribute] forKey:currencyName]; } // Obtain the next attribute attribute = attribute->next; } // if the element has child elements, process them if (element->firstChild) [self traverseElement:element->firstChild]; // Obtain next sibling element } while ((element = element->nextSibling)); }
回答2:
I realise this question has been answered already, but for anyone else looking for a solution to this same issue, there's also a great JSON solution available at openexchangerates.org as well.
回答3:
My first port of call would be to find a webservice that provides currency rates with a public API. Then you'd need to integrate some functionality into your app that communicates with the API in order to get the information you need.
There might be some services that offer the exchange rates in an RSS feed or similar feed. You could then parse the XML downloaded from that feed into some objects that you can use in your app.