Downloading Stock quotes to App

心已入冬 提交于 2019-12-05 06:14:57

问题


I'm new to iOS programming and want to display stock quotes and stock details in the iPad app I'm designing. I am looking at two options Im looking at google finance or yahoo finance.

How would I go about sending a url request to their sites and then handling the reply in Xcode?

I see that yahoo have a YQL service but I can't wrap my head around how I would get it to work despite reading some yahoo documentation online so Im looking to avoid it. Ive had a look at the google api but I can't figure out how to integrate it and get it to request stock details. I've decided to go through the url request route and try to make up some basic code to handle the reply from a url request as this seems like the simplest option (though I don't know how this will work when searching for stocks).


回答1:


Just deleted my whole answer and rewrote it to avoid confusion:

I looked into how to use YQL to query the yahoo finance API and here's what I ended up with:

Here is the completed code to fully formulate the request string. You can throw this directly into the NSURL for a NSMutableURLRequest and will get a json response. This code will fetch every property of each ticker. To change this you will need to specify individual properties instead of the * in this bit in the prefix (select%20*%20). I took part of it from the sample code in this post. I modified the code to fit into an asynchronous request (also changed it slightly because part of it seemed outdated and wasn't working.

#define QUOTE_QUERY_PREFIX @"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20("
#define QUOTE_QUERY_SUFFIX @")%0A%09%09&env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json"

+ (NSString *)formulateYQLRequestFor:(NSArray *)tickers
{
    NSMutableString *query = [[NSMutableString alloc] init];
    [query appendString:QUOTE_QUERY_PREFIX];
    for (int i = 0; i < [tickers count]; i++) 
    {
        NSString *ticker = [tickers objectAtIndex:i];
        [query appendFormat:@"%%22%@%%22", ticker];
        if (i != [tickers count] - 1)
        {
            [query appendString:@"%2C"];
        }
    }
    [query appendString:QUOTE_QUERY_SUFFIX];

    return query;
}

You would invoke this by doing something like:

NSArray *tickerArray = [[NSArray alloc] initWithObjects:@"AAPL", @"VZ", nil];
NSString *queryURL = [MyClass formulateYQLRequestFor:tickerArray];

Use this answer to see how to formulate the request and consume the json that comes back. Essentially the part you need to change is

NSURL *url = [NSURL URLWithString:queryURL];

You're also not sending JSON over so you should to change the request to reflect that.




回答2:


My ScriptScraper tool does something similar, it loads the stocks into an Excel table.

cheers, Martin



来源:https://stackoverflow.com/questions/10522440/downloading-stock-quotes-to-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!