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

最佳答案

这是有关此内容的blog post,但是,如果使用TBXML,则可以使用以下方法进行操作。

他们执行以下操作:


假设您已经将一个可变的字典对象作为一个名为exchangeRates的类属性
设置欧元作为基本汇率(值1.0)
调用欧洲中央银行的汇率XML提要并对其进行解析。
调用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));
}

10-08 16:41