How to avoid double location prompt with WKWebView iOS apps (Xcode/Swift)

Woman using gps navigation system.

Introduction: What are location services in general?

Location services are the technologies that provide information about a person’s location. They can be used to track someone and also to provide relevant information.

Some of the most common location services are GPS, geolocation, and cell tower identification. These technologies can be used for various purposes, such as tracking people, delivering relevant offers based on location, or even finding your lost phone.

How do location services work in HTML5?

HTML5 geolocation is a JavaScript API that provides developers with the ability to get the geographic location of the device. The Geolocation API is a W3C specification that defines an interface for retrieving geographical location information associated with a hosting device. It is designed to enable web applications to request and receive this information in response to user interaction and page load without requiring continuous polling. So to sum it up, HTML5 provides a way for web pages to request the user’s location and then use that information to display content relevant to that location. HTML5 geolocation is a relatively new technology that has been around since 2010. It allows web pages to request the user’s location and then use that information to display content relevant to that location. For example, if you were looking at an article about shopping in Chicago, a browser might show you ads for stores in your area or news articles about Chicago-area events.

How about HTML5 Geolocation in WebView-based apps on iOS?

The iOS webview location permission prompt is a particular case: The prompt appears when you are using the webview to view a website and asks for permission to use your current location.

The prompt can be declined by tapping “Don’t Allow”. This will stop the app from accessing your location data, but it will disable the web app’s features, like geolocation-based searches and maps within the webview. But there is a problem. When a user first installs an iOS app with WebView location-based services enabled, the app will ask for location permission and then give you a prompt to allow or deny each time you open a page in the view. This differs from how Android handles this prompt, where you’re only prompted once when installing the app. So on iOS, this location permission request comes every single time the website requests the app for the first time (after app launch) and in addition to the general permission dialog for the app. This procedure creates a kind of double permission dialog that also reveals the URL and the app as a WebView app.

The solution for double location prompts in WKWebView-based apps

So how to prevent your WKWebView-based app from repeatedly asking for permission again and again? Good news: The new WebViewGold update (Swift app template for Xcode) from August 2022 has introduced a prompt that only once asks for permission to access the customer’s location (and even without revealing the URL). After allowing, the user will never be asked again, even after a complete restart of the app. So when a user first installs a WebViewGold-based app from the Apple App Store, the app will ask for location permission at first run, then never again. This will work if the geolocation API is triggered using a button or a link. 

Download WebViewGold