Skip to content

Instantly share code, notes, and snippets.

@zaersk
Last active August 29, 2015 14:04
Show Gist options
  • Save zaersk/dff2acb7df00c4648597 to your computer and use it in GitHub Desktop.
Save zaersk/dff2acb7df00c4648597 to your computer and use it in GitHub Desktop.
BM
NSString *textString = @"APPL";
NSString *query = [NSString stringWithFormat:@"select * from yahoo.finance.quotes where symbol in (\"%@\")", textString];
NSString *escapedStoreUrl = [@"store://datatables.org/alltableswithkeys" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSString *escapedQuery = [query stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
NSString *urlString = [NSString stringWithFormat:@"http://query.yahooapis.com/v1/public/yql?q=%@&env=%@&format=json", escapedQuery, escapedStoreUrl];
NSMutableURLRequest *mutableRequestGET = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[mutableRequestGET setHTTPMethod:@"GET"];
NSData *dataResponse = [NSURLConnection sendSynchronousRequest:mutableRequestGET returningResponse:nil error:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:dataResponse options:kNilOptions error:&error];
// Enumerate through JSON
NSDictionary *productSets = [[[json objectForKey:@"query"] objectForKey:@"results"] objectForKey:@"quote"];
/* If you want to get one key independently, replace the id
* of objectForKey with "symbol", "AverageDailyVolume", etc.
* (and uncomment the line below.!)
*/
//NSString *lastTradeDate = [productSets objectForKey:@"LastTradeDate"];
for (NSString *sub in productSets) {
NSLog(@"%@: %@",sub,[productSets objectForKey:sub]);
}
dispatch_async(dispatch_get_main_queue(), ^{
// Update UI accordingly with data from above.
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment