Getting the user’s physical location without location services

By | June 28, 2016

It has been a while since my last blogpost (due to various reasons: preparing wedding and talk at Techorama, meetings with the architect for the construction of our house,…)! But I finally found a bit time to write another blogpost. This time it is a very simple and short one about ‘magically’ getting the user’s location without using location services on the mobile device.

Intro

A customer where I’m currently working at, wanted to know if it would be possible to ‘block’ the usage of their app, once the user tries to use it outside the country. Immediately we started thinking about using location services and checking the user’s location. But, our customer didn’t like that idea and wanted to know if there weren’t any alternatives.

Getting the location ‘magically’

It sounded almost like we should do some magic in order to accomplish this. Eventually, there were all kinds of proposals like for example checking the user’s IP address in the backend. But, again, the custom didn’t like that thought as it would require a change in the backend and due to all kinds of reasons, this wouldn’t be an option.

But the IP thing sounded like a good starting point… And as our backend could not be used for this, let’s use an other one! 🙂

There is this little website named ‘Google’, a search engine. When you browse to it via google.com, you will be redirected to your country specific page. So for example, as I live in Belgium, when I browse to google.com, I get redirected to google.be. This is exactly what we need! If we would just do a GET to google.com and look in the response what the URI is where we landed on, then we could determine in which country the user is at the particular moment. Let’s code this!

Show me the code

This is how we can do this in UWP:

We can, of course, also do this in Xamarin if we want.

And that’s it! That’s all we have to do in order to check if the user of the app is in a particular country.

Side note

In the end, our customer decided not to release this ‘feature’, and therefore this feature has not been tested throughout. So I have no clue whether this works as we expect, if it is 100% waterproof or how this behaves ‘in the wild’. So, use it at your own risk! 😉

Thoughts or remarks are welcome in the comments area below!

 

Thanks to my colleague Jonathan for the idea!

One thought on “Getting the user’s physical location without location services

Leave a Reply