Objective-C Requires Chilkat v11.0.0+
Objective-C
Look up an IP address with the ip-api free endpoint
See more Geolocation Examples
This example uses the free, keyless ip-api endpoint for non-commercial use. The free service supports HTTP and is limited to 45 requests per minute per source IP. Its Pro service provides HTTPS and commercial use. ip-api Pro.
Always include status and message when selecting fields. Invalid or private addresses can produce status=fail inside a JSON response. Retain the X-Rl and X-Ttl response values between calls, and pause further requests when X-Rl reaches zero.
IP geolocation estimates a network location. VPNs, mobile gateways, and anycast routing can affect the result. City and coordinate fields can be missing or approximate. The sample address 8.8.8.8 is a public DNS address used to demonstrate the request.
Chilkat Objective-C Downloads
#import <NSString.h>
#import <CkoHttp.h>
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <CkoHttpResponse.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Use a public IP address. Provider results can change as their databases are updated.
NSString *ipAddress = @"8.8.8.8";
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.ConnectTimeout = [NSNumber numberWithInt:10];
http.ReadTimeout = [NSNumber numberWithInt:30];
http.Accept = @"application/json";
// The free endpoint is HTTP-only. This single lookup sends no API credentials.
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"http://ip-api.com/json/"];
[url Append: [http UrlEncode: ipAddress]];
CkoJsonObject *parameters = [[CkoJsonObject alloc] init];
[parameters UpdateString: @"fields" value: @"status,message,query,country,countryCode,regionName,city,lat,lon,timezone"];
CkoHttpResponse *response = [[CkoHttpResponse alloc] init];
success = [http HttpParams: @"GET" url: [url GetAsString] json: parameters response: response];
if ([response.StatusCode intValue] == 0) {
// Request diagnostics can contain an API key. Report transport failure without logging credentials.
NSLog(@"%@",@"No HTTP response. Check connectivity, DNS, and TLS configuration.");
return;
}
// Save these values if the application will make further requests.
NSString *remaining = [response GetHeaderField: @"X-Rl"];
if (remaining == @"0") {
NSLog(@"%@%@%@",@"Rate limit reached. Wait ",[response GetHeaderField: @"X-Ttl"],@" seconds before the next request.");
}
CkoJsonObject *json = [[CkoJsonObject alloc] init];
BOOL parsed = [json Load: response.BodyStr];
if ((success != YES) || ([response.StatusCode intValue] < 200) || ([response.StatusCode intValue] >= 300)) {
NSLog(@"%@%d",@"Lookup failed. HTTP ",[response.StatusCode intValue]);
if (parsed == YES) {
if ([[json JsonTypeOf: @"message"] intValue] == 1) {
NSLog(@"%@",[json StringOf: @"message"]);
}
}
return;
}
if (parsed != YES) {
NSLog(@"%@",@"Expected a JSON object in the lookup response.");
return;
}
if ([json StringOfEquals: @"status" value: @"success" caseSensitive: YES] != YES) {
NSLog(@"%@",@"The service could not complete the lookup.");
if ([[json JsonTypeOf: @"message"] intValue] == 1) {
NSLog(@"%@",[json StringOf: @"message"]);
}
return;
}
if (([[json JsonTypeOf: @"query"] intValue] != 1) || ([json StringOfEquals: @"query" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"The response did not contain the expected IP address.");
return;
}
NSLog(@"%@%@",@"IP address: ",[json StringOf: @"query"]);
if (([[json JsonTypeOf: @"country"] intValue] == 1) && ([json StringOfEquals: @"country" value: @"" caseSensitive: YES] != YES)) {
NSLog(@"%@%@",@"Country: ",[json StringOf: @"country"]);
}
else {
NSLog(@"%@",@"Country: unavailable");
}
if (([[json JsonTypeOf: @"countryCode"] intValue] == 1) && ([json StringOfEquals: @"countryCode" value: @"" caseSensitive: YES] != YES)) {
NSLog(@"%@%@",@"Country code: ",[json StringOf: @"countryCode"]);
}
else {
NSLog(@"%@",@"Country code: unavailable");
}
if (([[json JsonTypeOf: @"regionName"] intValue] == 1) && ([json StringOfEquals: @"regionName" value: @"" caseSensitive: YES] != YES)) {
NSLog(@"%@%@",@"Region: ",[json StringOf: @"regionName"]);
}
else {
NSLog(@"%@",@"Region: unavailable");
}
if (([[json JsonTypeOf: @"city"] intValue] == 1) && ([json StringOfEquals: @"city" value: @"" caseSensitive: YES] != YES)) {
NSLog(@"%@%@",@"City: ",[json StringOf: @"city"]);
}
else {
NSLog(@"%@",@"City: unavailable");
}
if (([[json JsonTypeOf: @"timezone"] intValue] == 1) && ([json StringOfEquals: @"timezone" value: @"" caseSensitive: YES] != YES)) {
NSLog(@"%@%@",@"Time zone: ",[json StringOf: @"timezone"]);
}
else {
NSLog(@"%@",@"Time zone: unavailable");
}
// Missing coordinates stay unavailable. A numeric zero is a valid coordinate.
if (([[json JsonTypeOf: @"lat"] intValue] == 2) && ([[json JsonTypeOf: @"lon"] intValue] == 2)) {
NSLog(@"%@%@%@%@",@"Coordinates: ",[json StringOf: @"lat"],@", ",[json StringOf: @"lon"]);
}
else {
NSLog(@"%@",@"Coordinates: unavailable");
}