locked
How to detect fake location in Xamarin Forms RRS feed

  • Question

  • User376589 posted

    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

    BELOW IS SHARED .NET CODE 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 //
    }

    BELOW IN ANDRIOD MAIN ACTIVITY ``` [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 else 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.

    Sunday, January 6, 2019 1:07 PM

All replies

  • User365590 posted

    Got the same issue, maybe isFromMockProvider is unreliable. You could try using KlaasNotFound's solution here: https://stackoverflow.com/questions/37803398/detecting-mock-location-not-working-android

    Tuesday, January 8, 2019 9:08 AM
  • User242601 posted

    @premjangra, @DianaZ, Now, it's much easy from the Xamarin Essentials and can be found here.

    You can detect this by using the IsFromMockProvider property on any Location object.

    Regards, Yousuf.

    Friday, June 21, 2019 3:10 AM