Location Services
If your app requires access to the device location in Android, you will need to enable this permission and then call a JavaScript Bridge command to request permission at runtime. For iOS, permission is only prompted at runtime. But to avoid both the app and your website prompting twice for location permission, you will need to use the function median_geolocation_ready() which is called when location services are initialized within the app.
Developer Demo
Display our demo page in your app to test during development https://median.dev/location-services/
Avoid double location permission prompt on iOS
Your iOS app will override your website's geolocation permission prompt so that only the app will ask for permission, and a double permission prompt is avoided.
However, the web-based geolocation is overridden with the native one only after the webpage has loaded. So there is a potential timing issue if you call the JavaScript geolocation request before the native location services has been initialized.
The solution is that once the app is finished doing the native function binding, a JavaScript callback function median_geolocation_ready()
is called within the web environment by the native layer of the app.
For example:
// Callback function automatically called when location services initialized in iOS app
function median_geolocation_ready() {
navigator.geolocation.getCurrentPosition(locationSuccess, locationError, locationOptions);
}
// Use callback function as a helper function and call immediately if not in iOS app
if (!navigator.userAgent.includes('MedianIOS')) {
median_geolocation_ready();
}
Android Runtime Location Services Permission
Prompt for location permission
Ensure you have Location Services permission enabled in your app configuration, then prompt for permission at runtime by using the following Median JavaScript Bridge command.
↔️Median JavaScript Bridge
median.android.geoLocation.promptLocationServices();
If permission for location services has previously been granted by the user the above bridge command will have no effect. Otherwise the user will be shown a dialog asking them to provide permission.
Check current permission status
You can check the current permission status using Median JavaScript Bridge command. For this command, provide a callback function or otherwise it returns a promise.
↔️Median JavaScript Bridge
median.android.geoLocation.isLocationServicesEnabled({'callback':function}); // Return value: { "enabled": true | false }
Resources
Location Services Demo App
Background Location Native Plugin
Updated 8 months ago