omma2289
omma2289

Reputation: 54639

Phonegap IOS geolocation permission alert not showing

I'm developing an IOS app in phonegap and I want to get the user's permission for location services upon loading. The problem is that the alert asking for permission doesn't appear until I resume the app (press home and open app again) or wait a couple of minutes, which is not acceptable.

I'm using the deviceready event like so:

document.addEventListener("deviceready", deviceReady, false);
function deviceReady() {
navigator.geolocation.getCurrentPosition(onLocationSuccess,
                                         onLocationError
                                         );
}

If I don't wait for deviceready the alert appears fine but I get double prompted with a message like this one:

/var/mobile/Applications/XXXX-XXXX-XXXX-XXXXXXXXXXX/AppName.app/www/index.html would like to use your current location

Any ideas to get this working?

Upvotes: 3

Views: 3303

Answers (4)

David Morales
David Morales

Reputation: 11

for the permission dialog on iOS use this code in your config.xml

<gap:config-file platform="ios" parent="NSLocationAlwaysUsageDescription" overwrite="false">
<array>
  <string>NSLocationAlwaysUsageDescription</string>
</array>

@AAhad for your problem when the user close the dialog or alert, you can check the option is available and you can talk with the user with new alert with your request if you need to active the GPS.

document.addEventListener("deviceready", onDeviceReady, false);


function onDeviceReady() {
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}

// onSuccess Geolocation
//
function onSuccess(position) {
  //Code for your GPS
}

// onError Callback receives a PositionError object or if your user close de dialog or cancel de permission
//
function onError(error) {
        alert('Error please check your GPS on Settings: '    + error.code    + '\n' +
              'Please Active Your GPS' + error.message + '\n');
    }

Upvotes: 0

David Morales
David Morales

Reputation: 11

this solution is perfect for me:

<gap:config-file platform="ios" parent="NSLocationAlwaysUsageDescription" overwrite="false">
    <array>
      <string>NSLocationAlwaysUsageDescription</string>
    </array>
</gap:config-file>

Upvotes: 0

CodeGems
CodeGems

Reputation: 559

I was fighting with this issue for few days and I finally got the solution. Seems like its not plugin nor corvoda version to blame. Try adding this Content-Security-Policy entry to your index.html inside head section:

<meta http-equiv="Content-Security-Policy" content="default-src * gap://ready file:; style-src 'self' 'unsafe-inline'; img-src 'self'

data:; script-src * 'unsafe-inline' 'unsafe-eval'">

Important part is

default-src * gap://ready file:;

gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication.

I hope it helps.

Upvotes: 2

AAhad
AAhad

Reputation: 2845

Though you have solved the issue but following will help others in future.

In order to show a permission dialog on iOS, you have to configure below in the config.xml.

<gap:config-file platform="ios" parent="NSLocationAlwaysUsageDescription" overwrite="false">
    <array>
      <string>NSLocationAlwaysUsageDescription</string>
    </array>
</gap:config-file>

I use this and it works well.

Upvotes: 0

Related Questions