我在哪里可以找到更新的实时汇率?

时间:2009-07-28 05:09:01

标签: iphone currency

如何将实时货币汇率与我的iPhone应用程序相关联?首先,任何人都知道我可以获得汇率的任何网站吗?第二,如何将其链接到我的应用程序?我想做这个应用程序的功能。 http://the-dream.co.uk/currencee/

4 个答案:

答案 0 :(得分:1)

以下是关于此的blog post,但回顾一下,如果您使用TBXML,则可以使用以下方法执行此操作。

他们执行以下操作:

  1. 假设您已将可变字典对象设为名为 exchangeRates 的类属性
  2. 将欧元设定为基准利率(价值1.0)
  3. 拨打欧洲中央银行的汇率XML Feed并对其进行解析。
  4. 在调用loadExchangeRates()方法后,您可以通过执行以下操作获取特定汇率:

    NSDecimalNumber *rate = [NSDecimalNumber decimalNumberWithString:[self.exchangeRates objectForKey:@"USD"]];  
    
  5. 以下是方法:

    - (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));
    }
    

答案 1 :(得分:1)

我意识到这个问题已经得到了解答,但对于其他寻求同一问题解决方案的人来说,openexchangerates.org也提供了一个很棒的JSON解决方案。

答案 2 :(得分:0)

我的第一个停靠点是找到一个使用公共API提供货币汇率的网络服务。 然后,您需要将一些功能集成到与API通信的应用程序中,以获取所需的信息。

可能有一些服务在RSS Feed或类似Feed中提供汇率。然后,您可以将从该Feed下载的XML解析为可在您的应用中使用的一些对象。

答案 3 :(得分:0)

英镑对其他货币的平均月度汇率,您可以在链接-中找到xml-

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

然后您可以将此xml加载到列表中并在代码中使用-

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);
        }