2

How do I determine what country an IP address is located in?

Jasarien
  • 58,279
  • 31
  • 157
  • 188
Love Thailand
  • 769
  • 10
  • 12
  • @Szu How has that question got anything to do with getting the IP Address? – Popeye Aug 05 '14 at 08:17
  • The OP needs to know which network interface they want the address of, before using that code. – Droppy Aug 05 '14 at 08:20
  • 1
    Download English IP2Location CSV database from here http://www.ip2location.com/free/country-multilingual and parse the CSV with your IP. – iphonic Aug 05 '14 at 08:22
  • why don't you use the the geolocation service to fetch the geographical name of the street, the city or the country? – holex Aug 05 '14 at 08:26

1 Answers1

10

Thank you for comment!

I'm find answer from topic How to get user external IP and Geolocation Programmatically in iOS and edit code to result:

NSMutableURLRequest *requestHTTP = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://ip-api.com/json"]
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData  timeoutInterval:10];

[requestHTTP setHTTPMethod:@"GET"];
[requestHTTP setValue: @"text/json" forHTTPHeaderField:@"Accept"];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:requestHTTP];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"IP response: %@ ",responseObject);
    NSString *myIP = [responseObject valueForKey:@"query"];
    NSLog(@"IP: %@", myIP);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", [error localizedDescription]);

}];
[op start];

Thank you!

Love Thailand
  • 769
  • 10
  • 12
  • 2
    You need to pay attention to their policy since you are using ip-api.com. http://ip-api.com/docs/ – leonard May 12 '15 at 08:23