Forum Xamarin.Forms

How to detect fake location in Xamarin Forms

premjangrapremjangra Member
edited January 2019 in Xamarin.Forms

Hi Team,

I am developing one xamarin form app in which i am taking current lat long of user (GPS) using xamarin essentials. Here i want to stop user sending fake location using MOCK setting or Any Fake GPS APP.

Android:- To achieve this , i have created dependency service but always getting mock location as false using IsFromMockProvider.
I have tested it by Switch ON the Mock location APP named "FAKE GPS". My app still capturing fake location when user use it otherwise it gets real location.

Below is the code i have written

1. Interface added

using System;
namespace SmartAttendance.Interfaces
    public interface IMockLocation
       Boolean IsMockLocation(Xamarin.Essentials.Location location);

//Calling Part

 if(Device.RuntimePlatform == Device.Android)
                  mock =  DependencyService.Get<IMockLocation>().IsMockLocation(position); //Here always getting mock as false even tried fake gps app //                   


[assembly: Xamarin.Forms.Dependency(typeof(SmartAttendance.Droid.Mock))]
namespace SmartAttendance.Droid
    public class Mock : IMockLocation
            public  Boolean IsMockLocation(Xamarin.Essentials.Location location) //Copying ESSENTIAL VARIABLE TO ANDRIOD LOCATION VARIABLE
                Boolean isMock = false;
                Context context = Android.App.Application.Context;
                Location alocation = new Location("");
                alocation.Latitude = location.Latitude;
                alocation.Longitude =location.Longitude;
                alocation.Accuracy = (float)location.Accuracy;
                alocation.Altitude = (float) location.Altitude;
                alocation.Speed = (float) location.Speed;
                alocation.Time =  location.Timestamp.Ticks;
                if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBeanMr2) 
                    isMock = alocation.IsFromMockProvider;  //using marshmallow so this part is executing
                    isMock = Settings.Secure.GetString( context.ContentResolver, Settings.Secure.AllowMockLocation).Equals("0"); //this is below jellybean
                return isMock;


FOR IOS : NOT tried any thing yet. As i know its not much easy to set mock location in IOS as compare to ANDRIOD. But If any body can provide any thing it will be higly appreciable

So, Overall tried lot of things but couldn't succeed. Please help what wrong i am doing to stop sending Fake location.


Sign In or Register to comment.