Where can I find updated, live exchange rates?

2019-05-18 15:12发布

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/

4条回答
Fickle 薄情
2楼-- · 2019-05-18 15:34

Average monthly exchange rates of GBP to other currencies you can find as xml in link -

string url = "http://www.hmrc.gov.uk/softwaredevelopers/rates/exrates-monthly-" + 
    month2SymbolsYear2SymbolsString + ".xml";

then you can load this xml into lists and use in your code -

string xmlStr;
        using (var wc = new WebClient())
        {
            xmlStr = wc.DownloadString(url);
        }


        var doc = XDocument.Parse(xmlStr);

        var currenciesCodes = doc.Root.Elements().Select(x => x.Element("currencyCode"));
        var rates = doc.Root.Elements().Select(x => x.Element("rateNew"));
        List<string> currenciesCodesList = new List<string>();
        foreach (var code in currenciesCodes)
        {
            currenciesCodesList.Add(code.Value);
        }
        List<double> currenciesRatesToGBPList = new List<double>();

        foreach (var rate in rates)
        {
            double rateDouble;
            if (!Double.TryParse(rate.Value, out rateDouble))
            {
                errorMessage = "During monthly average exchanges rates loading from page" + "\r\n" +
                               url + "\r\n" +
                               "program found text value - " + rate.Value + "\r\n" +
                               "which can't be converted to double value" + "\r\n" +
                               "Program can't correctly form reports and will end now.";
                return errorMessage;
            }
            currenciesRatesToGBPList.Add(rateDouble);
        }
查看更多
对你真心纯属浪费
3楼-- · 2019-05-18 15:36

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.

查看更多
▲ chillily
4楼-- · 2019-05-18 15:41

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:

  1. Assume you have made a mutable dictionary object as a class property called exchangeRates
  2. Set's EUR as the base rate (value of 1.0)
  3. Call the European Central Bank's exchange rate XML feed and parses it.
  4. 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));
}
查看更多
来,给爷笑一个
5楼-- · 2019-05-18 15:41

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.

查看更多
登录 后发表回答