iPhone Objective-C Simplest Way to get a stock quote [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this questionIm wondering what is the simplest way to get the current price of a stock from say yahoo finance (or similar) in objective-C For the iPhone SDK.
Simple is the key开发者_如何学编程, I am looking for current price, and days movement.
I havent had much luck finding an iPhone code example or library.
regards
Use an NSURLRequest
object to retrieve the data at this address:
http://download.finance.yahoo.com/d/quotes.csv?s=AAPL&f=sl1d1t1c1ohgv&e=.csv
Using [NSString stringWithFormat:]
to change the AAPL to the stock ticker you want to use. The retrieved data is in CSV format so you will need to parse that to get the individual values you require. This can be done in this simple case using [NSString componentsSeparatedByString: @","]
to retieve an array which you can parse using two loops.
The simplest code snippet for this I know is along the lines of:
NSLog(@"%@", [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=BP.L&f=sl1d1t1c1ohgv&e=.csv"]]);
It retrieves BP's share price in London and prints it to the console.
For a full code example of this, check out the AAPLot sample application in the Core Plot framework. It downloads stock data and plots it with open-high-low-close information, as well as trading volume.
You could probably get a lot of your answers from the Yahoo Developer Network, in the Finance section.
The Quandl Stock API is free and let's you retrieve Yahoo or Google finance data. In addition to CSV, it provides the data in some more modern formats like JSON and XML. Here's how to retrieve for CSV:
https://www.quandl.com/api/v1/datasets/WIKI/AAPL.csv
Here's the small change to retrieve in JSON format:
https://www.quandl.com/api/v1/datasets/WIKI/AAPL.json
No API key is needed, but getting an API key is free and allows you to make up to 5000 calls per hour.
Another big plus is that the same API can be used to retrieve fundamental data about companies.
精彩评论