Get Started With IP2location Cocoa Library
Dependencies
This library requires IP2Location BIN database to function. You may download the BIN database at
IP2Location LITE BIN Data (Free): https://lite.ip2location.com
IP2Location Commercial BIN Data (Comprehensive):https://www.ip2location.com
Installation
Download and decompress ip2location-cocoa-x.x.x.zip.
-
You can build your project by using either Xcode or command line/makefile.
For Xcode usage:
Add ip2location.framework to your Xcode project by dragging and dropping it into Project Navigator.
In your appropriate target, under "Build Settings", select "Runpath Search Paths" in the "Linking" category, and enter "@loader_path/../Frameworks" (without the quotes).
Build your project.
For command line or makefile usage:
Copy ip2location.framework to your working directory.
Build and link your target with following options added (without the quotes): "-F. -L. -Wl,-framework,ip2location -Wl,-framework,Foundation".
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;
}