简体   繁体   中英

Testing Location Services on iOS Device

I'm building an app which will list the nearest product dealers to the users current location.

When testing on my device iPhone 6 & iPhone 4S I can see that my app doesn't have permission to get the location but when I go to settings I can't see my test app listed to grant it that permission.

Is this due to the way the app is installed when "Run" via Appcelerator Studio? How can I grant a test app permission please?

My code is:

function getCurrentPhoneLocation(callback)
{
    Titanium.API.info("get phone location " + Ti.Geolocation.locationServicesEnabled);
    if(Ti.Geolocation.locationServicesEnabled)
    {
        Titanium.API.info("GPS permissions: " + Ti.Geolocation.locationServicesAuthorization + " (" + Ti.Geolocation.AUTHORIZATION_ALWAYS + " | " + Ti.Geolocation.AUTHORIZATION_AUTHORIZED + " | " + Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE +  ")");
        if (Ti.Geolocation.locationServicesAuthorization == Ti.Geolocation.AUTHORIZATION_ALWAYS || Ti.Geolocation.locationServicesAuthorization == Ti.Geolocation.AUTHORIZATION_AUTHORIZED || Ti.Geolocation.locationServicesAuthorization == Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE)
        {
              Titanium.API.info("Got permissions - lets go!");

              Ti.Geolocation.purpose = 'Get current location';        

              var currentPhoneLocation = {};

              Ti.Geolocation.getCurrentPosition(function(e){
                  Titanium.API.info("from pos: " + JSON.stringify(e));
                  if(e.success === false) {
                      Ti.API.error('Error:' + e.error);
                      alert("Location is currently unavailable");
                      callback( false );
                  } else {

                      currentPhoneLocation.longitude = e.coords.longitude;
                      currentPhoneLocation.latitude = e.coords.latitude;
                      Ti.API.info("Returned Cords: " + JSON.stringify(currentPhoneLocation));
                      callback(); 
                  }               
              });             
        } 
        else
        {
              Titanium.API.info("No APP permission");
              Titanium.UI.createAlertDialog({title:'Location Service', message:'Please grant this app permission to get your location.'}).show();
              callback( false );
        }
    }
    else
    {
          Titanium.API.info("No GPS available");
          Titanium.UI.createAlertDialog({title:'Location Service', message:'Please turn on your location services.'}).show();
          callback( false );
    }
}

You can see my trace code is showing that Ti.Geolocation.locationServicesAuthorization is returning '0'.

[INFO] :   get phone location true
[INFO] :   GPS permissions: 0 (3 | 3 | 4)
[INFO] :   No APP permission
[INFO] :   Recieved location: false

No matter how you install it, preferences work the same. You still have to authorise it.

However, a lot of things have changed for permissions in the latest release, and I recommend looking at the documentation to set it up properly. You can find an example app on GitHub

Best way to check if it has been granted is through:

Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS);

Also, I recommend fetching location. The false is actually provided by you. If you granted the app permissions (verified in settings) this should work

Ti.GeoLocation.getCurrentPosition(callback(e){})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM