Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

Look up an IP country with MaxMind GeoIP

See more Geolocation Examples

Call the GeoIP Country web service using HTTP Basic authentication. Set the username to your numeric MaxMind account ID and the password to your license key. The account needs access to the web service, such as a trial or service credit. Both IPv4 and IPv6 are supported.

country describes the geolocated country. registered_country describes where the network is registered. The Country service returns country-level data. City-level lookups use the separately entitled /geoip/v2.1/city/ service. A GeoLite web-service account uses geolite.info as its host.

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.

Service documentation

Chilkat Objective-C Downloads

Objective-C
#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";
http.BasicAuth = YES;
http.Login = @"MAXMIND_ACCOUNT_ID";
http.Password = @"MAXMIND_LICENSE_KEY";
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://geoip.maxmind.com/geoip/v2.1/country/"];
[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: @"code"] intValue] == 1) {
            NSLog(@"%@",[json StringOf: @"code"]);
        }

        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 JsonTypeOf: @"traits.ip_address"] intValue] != 1) || ([json StringOfEquals: @"traits.ip_address" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"The response did not contain the expected IP address.");
    return;
}

NSLog(@"%@%@",@"IP address: ",[json StringOf: @"traits.ip_address"]);
if (([[json JsonTypeOf: @"country.names.en"] intValue] == 1) && ([json StringOfEquals: @"country.names.en" value: @"" caseSensitive: YES] != YES)) {
    NSLog(@"%@%@",@"Country: ",[json StringOf: @"country.names.en"]);
}
else {
    NSLog(@"%@",@"Country: unavailable");
}

if (([[json JsonTypeOf: @"country.iso_code"] intValue] == 1) && ([json StringOfEquals: @"country.iso_code" value: @"" caseSensitive: YES] != YES)) {
    NSLog(@"%@%@",@"Country code: ",[json StringOf: @"country.iso_code"]);
}
else {
    NSLog(@"%@",@"Country code: unavailable");
}

if (([[json JsonTypeOf: @"continent.names.en"] intValue] == 1) && ([json StringOfEquals: @"continent.names.en" value: @"" caseSensitive: YES] != YES)) {
    NSLog(@"%@%@",@"Continent: ",[json StringOf: @"continent.names.en"]);
}
else {
    NSLog(@"%@",@"Continent: unavailable");
}

if (([[json JsonTypeOf: @"registered_country.iso_code"] intValue] == 1) && ([json StringOfEquals: @"registered_country.iso_code" value: @"" caseSensitive: YES] != YES)) {
    NSLog(@"%@%@",@"Registered country code: ",[json StringOf: @"registered_country.iso_code"]);
}
else {
    NSLog(@"%@",@"Registered country code: unavailable");
}