Skip to content
Advertisement

getCurrentPosition in JS does not work on iOS

I have a page that contains a code that gets the current location from the device and load other stuff based on the location with this code:

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction);
} else {
    // Make API call to the GeoIP services
}

It works on all android devices that I tested, but on iOS and macOS, it’s not working. Neither if nor else. Seems like it stuck at getting the current location.

Any help?

Advertisement

Answer

iOS and macOS doesn’t give you the user’s location if they don’t allow it or if the system can not trust you. So:

– First (Probably your answer)

It works with the exact code you provided ONLY IF the host (aka the origin) is using https. That is the cause of permission alert not showing up due to your comment to an answer below.

Remember if you not use https, it will be stuck for about a minute and then returns an error about authentication failure. Use error to check that:

navigator.geolocation.getCurrentPosition(success, error, options)

It will tell you something like this:

[blocked] Access to geolocation was blocked over insecure connection to http://example.com.

Tip: You can refresh the page after you requested the location to skip the waiting process. But don’t forget to check the Preserve Log option in the inspector.

– Second:

If you are using https, then check your location settings for Safari, which might have been set to OFF somehow, you can change it here: Settings > Privacy > Location Services > Safari. This is not the default option, but maybe you changed it accidentally. So don’t worry about the users if this was the issue. And if you use Chrome or any other third party browsers, head to its settings and check for location access. It’s not there by default and appears only if location wanted at least once.

– Third:

If you are about to load your web inside an app using webView, make sure to add location permission descriptions to the info.plist file. Add NSLocationWhenInUseUsageDescription versus NSLocationAlwaysUsageDescription versus NSLocationUsageDescription as your needs.

For the sake of completeness, on iOS13, you can not get always permission. But this is not the point here and the point is you have to get the required permissions sometime before you need to get the location from GPS or it is not going to work at all.

Advertisement