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?
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.
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With