Get Started With IP2location Cocoa Library

Dependencies

This library requires IP2Location BIN database to function. You may download the BIN database at


Installation

  1. Download and decompress ip2location-cocoa-x.x.x.zip.

  2. You can build your project by using either Xcode or command line/makefile.

    For Xcode usage:

    1. Add ip2location.framework to your Xcode project by dragging and dropping it into Project Navigator.

    2. In your appropriate target, under "Build Settings", select "Runpath Search Paths" in the "Linking" category, and enter "@loader_path/../Frameworks" (without the quotes).

    3. Build your project.

    For command line or makefile usage:

    1. Copy ip2location.framework to your working directory.

    2. Build and link your target with following options added (without the quotes): "-F. -L. -Wl,-framework,ip2location -Wl,-framework,Foundation".

    3. Change linked framework path in your binary: "install_name_tool -change @rpath/ip2location.framework/Versions/A/ip2location ip2location.framework/Versions/A/ip2location binary_executable".


Sample Codes

Query geolocation information from BIN database

You can query the geolocation information from the IP2Location BIN database as below:

#import <Foundation/Foundation.h>
#import <ip2location/IP2Location.h>

int main(int argc, const char * argv[])
{

	@autoreleasepool {
		//open database
		IPLocation *locationDB = [[IPLocation alloc] initWithDataPath: @"data/IP-COUNTRY-SAMPLE.BIN"];

  //set memory cache type
  [locationDB setMemoryType: IP2LOCATION_MEMORY_CACHE];

  //search
  IPLocationRecord *rec =  [locationDB recordWithIP: @"8.8.8.8"];
  NSLog(@"%@", rec.countryShort);
	}
	return 0;
}

IP2Location Sample Database (BIN)

The sample BIN files have a limited scope and are only valid for the following IP ranges:

  1. IPv4 : 0.0.0.0 - 7.255.255.255
  2. IPv6 : 2a04:0000:0000:0000:0000:0000:0000:0000 - 2a04:ffff:ffff:ffff:ffff:ffff:ffff:fff