Search code examples
actionscript-3air

Getting the location of a user in AS3/Air app for mobile devices


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


Solution

  • 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.