1

I have an native app that I would like to store the location (just country would be fine) of where a user that logs into my app is from so I can customise some features for them.

I am using PHP as the backend. I have done some searching but nothing definitive found. Not sure how to get the IP of the device to be able to translate that to a location.

Thanks

puks1978
  • 3,667
  • 11
  • 44
  • 103

3 Answers3

1

Did you look on http://www.ip2location.com/ You can use it in you php backend to translate ip to address

Oferch
  • 71
  • 7
  • Thanks. Maybe I should have been a bit clearer in the question (updated). I need to know how to get the IP of the mobile device so I can then translate this to a location. – puks1978 Sep 09 '13 at 10:20
1

I had to do this for my previous project. Best bet is not to rely on IP address, especially on mobile devices (my phone's IP currently has me somewhere in northern Utah when I'm actually in Phoenix). Use the GeoLocation API. That will give you the latitude, longitude position of a user which is generally fairly accurate. You can then use a GeoCoding service to determine what country they are in using reverse-geocoding. Google's GeoCoding API is fantastic and their terms were recently (April-ish) relaxed substantially making it much easier to use. I can also recommend Nominatim from OpenStreetMaps (their terms are pretty restrictive, but you can use MapQuest's Nominatim service which is pretty much free to be used however you want)

EDIT: As Pier mentioned, an ANE is required to use NetworkInfo on mobile. If you attempt to use it on mobile without the ANE, it will crash your app.

If you are set on using IPs, however, you can obtain it through the NetworkInfo class. NetworkInfo.networkInfo.findInterfaces() will give you a bunch of NetworkInterface objects. You'll be looking for InterfaceAddress. I do warn you, however, I have had difficulty in the past getting this method to work on mobile, especially iOS. Additionally, I cannot remember if it returns the network IP (i.e. 192.168.1.17) or the actual IP that is connected to the web. The local IP will obviously do you no good.

Josh
  • 8,079
  • 3
  • 24
  • 49
  • 1
    BTW, the `NetworkInfo` class does not work on mobile and you need to use a native extension (ANE). – Pier Sep 09 '13 at 17:38
  • @Pier Yes. That is exactly what I was referring to when I said it was difficult to use on mobile. I could not remember exactly why, just that it was difficult. Thanks for pointing that out. – Josh Sep 09 '13 at 17:50
  • Thank you for your answer/comments. I am using Distriqt network ANE and just spoke to their developers however this ANE is unable to get the public IP of a mobile device, only internal IP which are no good. I was considering the GeoLocation if I couldn't get the IP but didn't want to add another feature to the app if possible. Distriqt suggested just a simple REMOTE_ADDR from my PHP script and that seemed to look OK...on the one test device I tried it on. More testing to be done first. – puks1978 Sep 09 '13 at 23:24
  • @puks1978 Again, IP addresses on mobile devices may not be accurate. As I mentioned, my phone's IP indicates I am in northern Utah when I am actually in Phoenix, AZ. You should use `GeoLocation`. That is the entire purpose of that API – Josh Sep 09 '13 at 23:48
  • @JoshJanusch I think you should edit your answer. The behaviour you describe only works on desktop. In a mobile device it will freeze the app. – Pier Sep 11 '13 at 00:31
  • @Pier Good call. Just added a note about that – Josh Sep 11 '13 at 03:42
0

I would use geolocation if I were you. if its a mobile device you can use gps or cell tower (less accurate) to get their location, but if thats not an option for you and you dont have the capability of using a server-side technology like php, asp, etc. to get the ip easily, maybe this post will help: How to find the ip address in as3?

Community
  • 1
  • 1
tamak
  • 1,541
  • 2
  • 19
  • 39