Objective-C
Objective-C
Finnhub API - Get Stock Quote
See more AI Examples
Demonstrates how to get a stock quote from the Finnhub API.Chilkat Objective-C Downloads
#import <NSString.h>
#import <CkoHttp.h>
#import <CkoHttpRequest.h>
#import <CkoHttpResponse.h>
#import <CkoJsonObject.h>
BOOL success = NO;
// Replace with your actual Finnhub API key.
NSString *apiKey = @"YOUR_FINNHUB_API_KEY";
NSString *symbol = @"AAPL";
CkoHttp *http = [[CkoHttp alloc] init];
// This is the URL without params.
NSString *urlWithoutParams = @"https://finnhub.io/api/v1/quote";
CkoHttpRequest *req = [[CkoHttpRequest alloc] init];
// Add params that will be sent in the URL.
[req AddParam: @"symbol" value: symbol];
[req AddParam: @"token" value: apiKey];
req.HttpVerb = @"GET";
// Send the request to get the JSON response.
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
success = [http HttpReq: urlWithoutParams request: req response: resp];
if (success == NO) {
NSLog(@"%@",http.LastErrorText);
return;
}
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[resp GetBodyJson: json];
int statusCode = [resp.StatusCode intValue];
NSLog(@"%@%d",@"response status code: ",statusCode);
json.EmitCompact = NO;
NSLog(@"%@",[json Emit]);
// Sample result:
// {
// "c": 248.8,
// "d": -4.09,
// "dp": -1.6173,
// "h": 255.493,
// "l": 248.07,
// "o": 253.9,
// "pc": 252.89,
// "t": 1774641600
// }
if (statusCode == 200) {
// Add the symbol to the top of the result.
[json AddStringAt: [NSNumber numberWithInt: 0] name: @"symbol" value: symbol];
// Rename members for clarification.
[json Rename: @"c" newName: @"currentPrice"];
[json Rename: @"d" newName: @"change"];
[json Rename: @"dp" newName: @"percentChange"];
[json Rename: @"h" newName: @"high"];
[json Rename: @"l" newName: @"low"];
[json Rename: @"o" newName: @"open"];
[json Rename: @"pc" newName: @"prevClose"];
[json Rename: @"t" newName: @"unixTime"];
NSLog(@"%@",[json Emit]);
}
else {
NSLog(@"%@",@"Failed");
}