12

I don't need precise location data, and I don't want the user to see the "This App wants to determine your location" Alert. I just need to determine the user's country, assuming they have an internet connection via network or wi-fi.

What is the best way to do this? Is there some way to use their IP?

This would only work if they had a carrier:

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

And NSLocale is not reliable in that the user can change it in their device settings. Can't use the device's language setting for the same reason. I need the country based on where they are physically located.

soleil
  • 12,133
  • 33
  • 112
  • 183
  • Since a user might be in airplane mode or not connected to a network, or the user could be using a proxy, there is no guarantee you can get accurate info from the network. – rmaddy Jul 11 '13 at 01:20

2 Answers2

1

You could get the user's IP address and use a geolocation database to guess their location based on that as described here: https://en.wikipedia.org/wiki/IP_address_location . If you are just concerned with region, you can probably find some data at regional internet registries (https://en.wikipedia.org/wiki/Regional_Internet_registry) websites to help you, since they control all the IP addresses for a particular region.

Ben Pious
  • 4,765
  • 2
  • 22
  • 34
  • How would you get around this issue - If I'm on my home wi-fi my IP is 192.168.x.x, and if I'm on my cell network my IP is 10.174.x.x? – soleil Jul 11 '13 at 00:44
  • You're right, you'll just be getting garbage for your ip address -- this would only work if you were a website or something. I would try some of the answers here to get the address of the router itself: http://stackoverflow.com/questions/2113580/objective-c-how-to-fetch-the-router-address?lq=1 . – Ben Pious Jul 11 '13 at 01:24
  • Your local IP address (eg: 192.168.x.x) is different than your external IP address. So if you're going to use an IP address to get user's location, you need to get his external IP address. – Ahmad Baraka Dec 07 '14 at 12:51
1

Refer this url How can whatsapp know country code upon first run on iphone.. Hope this question and the question in the url i have given works for you

Community
  • 1
  • 1
iOSdev
  • 553
  • 5
  • 22