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).
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.
My ScriptScraper tool does something similar, it loads the stocks into an Excel table.
cheers,
Martin