2

如何将实时货币汇率链接到我的 iPhone 应用程序?首先,有人知道我可以得到汇率的任何网站吗?其次,如何将其链接到我的应用程序?我想做这个应用程序所做的事情。http://the-dream.co.uk/currencee/

4

4 回答 4

1

这是一篇关于此的博客文章,但回顾一下,如果您使用TBXML,您可以使用以下方法进行操作。

他们执行以下操作:

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

    NSDecimalNumber *rate = [NSDecimalNumber decimalNumberWithString:[self.exchangeRates objectForKey:@"USD"]];  
    

以下是方法:

- (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));
}
于 2011-08-05T15:14:09.463 回答
1

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

于 2012-07-05T22:56:41.723 回答
0

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

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

于 2009-07-28T11:01:41.593 回答
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);
        }
于 2018-11-27T11:38:33.743 回答