Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - is a user in the United States?

Is there any way for me to detect whether or not a user is in the US without asking for their location? For example, when I go to Fandango on my computer it knows which county and state I'm in just based on my WiFi.

If I have a device, perhaps an iPod Touch, that doesn't have GPS, and I wanted to verify it is in the United States, and they are connected to WiFi... or if I had an iPhone and it was connected with 3G and I wanted to make sure the users were in the US, is there a way I can check that information without the little alert view "__ would like to use your current location"? I don't need exact coordinates, I just need to confirm that these users are in the USA (including Hawaii and Alaska)... locale?

like image 288
Albert Renshaw Avatar asked Aug 30 '12 21:08

Albert Renshaw


2 Answers

Ok, how do you do that with the IP.

So, geoplugin.net has it's amazing JSON api, it defaults the IP to the currently conection, so what you have to do is make a request to this address:

http://www.geoplugin.net/json.gp

Astunishing! It returns, for me, this data:

geoPlugin({
  "geoplugin_request":"201.6.226.233",
  "geoplugin_status":200,
  "geoplugin_city":null,
  "geoplugin_region":"São Paulo",
  "geoplugin_areaCode":0,
  "geoplugin_dmaCode":0,
  "geoplugin_countryCode":"BR",
  "geoplugin_countryName":"Brazil",
  "geoplugin_continentCode":"SA",
  "geoplugin_latitude":-23.473301,
  "geoplugin_longitude":-46.665798,
  "geoplugin_regionCode":27,
  "geoplugin_regionName":"São Paulo",
  "geoplugin_currencyCode":"BRL",
  "geoplugin_currencySymbol":"R$",
  "geoplugin_currencyConverter":2.0198
})

Well, now what you have to do is just parse this "JSON". It's not actually a JSON as it has this geoplugin( {data} ) wrapper. So you could lazily execute some filtering, removing those parts of a NSSTring, maybe.

I See you are in a hurry, so I took my spare time to write some code for you. It's very non-standard as I don't know if you are using any REST framework that would help, but here goes:

NSString *url = [NSString stringWithFormat:@"http://www.geoplugin.net/json.gp"];


    NSString *locationData = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]
                                                encoding:NSUTF8StringEncoding]];

    locationData = [locationData stringByReplacingOccurrencesOfString:@"geoPlugin(" withString:@""];
    locationData = [locationData stringByReplacingOccurrencesOfString:@")" withString:@""];

    //user some json parser here.
    if([[[locationData JSONValue] valueForkey:@"geoplugin_countryCode"] isEqualToString:@"US"]){

        //proceed.
    }
like image 191
jturolla Avatar answered Oct 03 '22 21:10

jturolla


I would first try to check the carrier's Mobile Country Code. It will tell you the numeric mobile country code for the user's cellular service provider and cannot be changed by the user unless they switch to a service provider in a different country.

#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netInfo subscriberCellularProvider];
NSString *mcc = [carrier mobileCountryCode];

This of course will not work with devices that are not connected to a mobile carrier (iPod Touch, some iPads, ect). Therefor, as a fall back, I would use or create my own IP geolocation API. You can get a free database that is 99.5% accurate on a country level. This will work great to detect the country of a device that doesn't have a mobile provider.

like image 42
Scott Bartell Avatar answered Oct 03 '22 22:10

Scott Bartell