capacitor-location-plugin
v1.2.6
Published
Capacitor plugin to check if location is enabled
Readme
Capacitor Location Plugin
A Capacitor plugin to check whether location services are enabled, listen for changes in real time, and detect fake / mock GPS (spoofing apps such as Lockito, Fake GPS, etc.).
Features
- Check if location services are enabled (
isEnabled) - Listen for location-services changes (
locationStatusChangedevent) - Detect mock / spoofed location (
checkMock) — on Android reads the mock flag from the fused provider (the source most apps use); on iOS 15+ readsCLLocation.sourceInformation.isSimulatedBySoftware. Catches a spoofed position even while the device is stationary - Open the device settings so the user can disable the mock-location app (
openDeveloperSettings) - Lightweight: the status check uses system broadcasts (no continuous updates)
Mock detection support: Android, and iOS 15+. On iOS below 15 and on web,
checkMockresolves{ isMock: false, available: false }.
Installation
npm install capacitor-location-plugin
npx cap syncThe mock detection uses com.google.android.gms:play-services-location, which the plugin declares as a dependency — no extra setup required.
Usage
import { LocationPlugin } from 'capacitor-location-plugin';Check if location services are enabled
const { isEnabled } = await LocationPlugin.isEnabled();Listen for location-services changes
const listener = await LocationPlugin.addListener(
'locationStatusChanged',
(status) => {
console.log('Location enabled:', status.isEnabled);
},
);
// later
listener.remove();Detect fake / mock GPS
const { isMock, available } = await LocationPlugin.checkMock();
if (available && isMock) {
// A mock-location app is actively feeding the position.
await LocationPlugin.openDeveloperSettings();
}Poll it on an interval (and re-check on app resume) to react when the user enables or disables a spoofing app:
setInterval(async () => {
const { isMock, available } = await LocationPlugin.checkMock();
if (available) blocked.value = isMock;
}, 5000);API
isEnabled()
isEnabled() => Promise<{ isEnabled: boolean }>Returns whether GPS/network location services are enabled.
initialize(options?)
initialize(options?: any) => Promise<{ isEnabled: boolean }>Registers the internal broadcast receiver and returns the current status. Call once before relying on locationStatusChanged.
checkMock()
checkMock() => Promise<{ isMock: boolean; available: boolean }>Requests a fresh location and reports whether it is mocked.
- Android: reads the fused provider (
Location.isMock()on API 31+,isFromMockProvider()below), falling back to theLocationManagerproviders if fused is unavailable. - iOS 15+: requests a one-shot location and reads
CLLocation.sourceInformation.isSimulatedBySoftware.
Fields:
isMock:truewhen the current position comes from a mock/simulated source.available:falsewhen the check could not run (no location permission, iOS below 15, or web) — treatisMockas inconclusive.
Requires location permission granted at runtime (ACCESS_FINE_LOCATION/ACCESS_COARSE_LOCATION on Android, when-in-use on iOS).
openDeveloperSettings()
openDeveloperSettings() => Promise<void>On Android, opens the developer settings screen (falls back to the main settings) so the user can turn off the selected mock-location app. On iOS, opens the app's settings page.
addListener('locationStatusChanged', ...)
addListener(
eventName: 'locationStatusChanged',
listenerFunc: (status: { isEnabled: boolean }) => void,
) => Promise<PluginListenerHandle>Android setup
Add the location permissions to your app's AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />iOS setup
In Info.plist, add usage descriptions:
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app requires access to your location while using the app.</string>Limitations
Mock detection relies on the OS simulated-location flag. It reliably catches spoofing apps on a non-rooted / non-jailbroken device. A rooted Android device running a mock-hiding module (Xposed/LSPosed) can suppress the flag; defeating that requires attestation (e.g. Play Integrity) and is out of scope for this plugin. iOS detection requires iOS 15+ (no public API exists below that).
License
MIT
