Skip to content

Commit

Permalink
fix: assume geolocation permission state in iOS <= 15 based on geoloc…
Browse files Browse the repository at this point in the history
…ation feature presence

Don't use unsupported permission queries.
  • Loading branch information
th0rgall committed Jan 19, 2024
1 parent 95d9b2b commit d02f0de
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/lib/components/Map/Map.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,32 @@
}
}
const geolocationPermission =
'geolocation' in navigator
? await navigator.permissions.query({ name: 'geolocation' }).then((result) => result.state)
: 'not-available';
let geolocationPermission: string;
// We noticed a bug where the map broke on iOS 15.2 due to "query" not existing on the permissions
// object; while it actually supports geolocation
//
// From: https://github.com/mapbox/mapbox-gl-js/blob/d8827408c6f4c4ceecba517931c96dda8bb261a1/src/ui/control/geolocate_control.js#L175-L177
// > navigator.permissions has incomplete browser support http://caniuse.com/#feat=permissions-api
// > Test for the case where a browser disables Geolocation because of an insecure origin;
// > in some environments like iOS16 WebView, permissions reject queries but still support geolocation
const geolocationObjectExists = !!navigator.geolocation;
// on iOS <= 16, I don't think we can know the state. But 'prompt' is safe to assume?
// because actually attempting a geolocation may result in a prompt / error / location
// The purpose of this `geolocationPermission` is to decide whether or not to add the
// geolocation control, and whether or not to try to trigger it automatically (see further on).
const assumedState = geolocationObjectExists ? 'prompt' : 'not-available';
if ('geolocation' in navigator && navigator?.permissions?.query !== undefined) {
// Query if possible
geolocationPermission = await navigator.permissions
.query({ name: 'geolocation' })
.then((result) => result.state)
// fall back to assumed state
.catch(() => assumedState);
} else {
// fall back to assumed state if queries are not possible
geolocationPermission = assumedState;
}
addMap();
Expand Down

0 comments on commit d02f0de

Please sign in to comment.