Look up an IP address with the DB-IP free API
See more Geolocation Examples
Use the DB-IP v2 free service to retrieve country, state or province, and city without an API key. This example uses HTTPS, which returned a successful free lookup during verification on November 20, 2026. The provider documents a limit of 500 daily requests for the free service. Paid services provide additional fields and quotas.
Check errorCode before reading the location. Free results have a smaller field set, and the application should handle missing location values.
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 <CkoHttpResponse.h>
#import <CkoJsonObject.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";
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://api.db-ip.com/v2/free/"];
[url Append: [http UrlEncode: ipAddress]];
CkoHttpResponse *response = [[CkoHttpResponse alloc] init];
success = [http HttpNoBody: @"GET" url: [url GetAsString] 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;
}
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: @"errorCode"] intValue] == 1) {
NSLog(@"%@",[json StringOf: @"errorCode"]);
}
if ([[json JsonTypeOf: @"error"] intValue] == 1) {
NSLog(@"%@",[json StringOf: @"error"]);
}
}
return;
}
if (parsed != YES) {
NSLog(@"%@",@"Expected a JSON object in the lookup response.");
return;
}
if (([json HasMember: @"errorCode"] == YES) || ([[json JsonTypeOf: @"error"] intValue] == 1)) {
NSLog(@"%@",@"The service could not complete the lookup.");
if ([[json JsonTypeOf: @"errorCode"] intValue] == 1) {
NSLog(@"%@",[json StringOf: @"errorCode"]);
}
if ([[json JsonTypeOf: @"error"] intValue] == 1) {
NSLog(@"%@",[json StringOf: @"error"]);
}
return;
}
if (([[json JsonTypeOf: @"ipAddress"] intValue] != 1) || ([json StringOfEquals: @"ipAddress" value: @"" caseSensitive: YES] == YES)) {
NSLog(@"%@",@"The response did not contain the expected IP address.");
return;
}
NSLog(@"%@%@",@"IP address: ",[json StringOf: @"ipAddress"]);
if (([[json JsonTypeOf: @"countryName"] intValue] == 1) && ([json StringOfEquals: @"countryName" value: @"" caseSensitive: YES] != YES)) {
NSLog(@"%@%@",@"Country: ",[json StringOf: @"countryName"]);
}
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: @"stateProv"] intValue] == 1) && ([json StringOfEquals: @"stateProv" value: @"" caseSensitive: YES] != YES)) {
NSLog(@"%@%@",@"State or province: ",[json StringOf: @"stateProv"]);
}
else {
NSLog(@"%@",@"State or province: unavailable");
}
if (([[json JsonTypeOf: @"city"] intValue] == 1) && ([json StringOfEquals: @"city" value: @"" caseSensitive: YES] != YES)) {
NSLog(@"%@%@",@"City: ",[json StringOf: @"city"]);
}
else {
NSLog(@"%@",@"City: unavailable");
}