Forum Xamarin.Forms

Xamarin Forms: How to check if GPS is on or off in Xamarin ios app?

SreeeeSreeee INMember ✭✭✭✭✭

Whenever I am opening my app I need to check the GPS is on or off. If the GPS is off, I need to redirect the user to the location settings page. I have done the android part using the dependency service like below.

ILocSettings

public interface ILocSettings
{
    void OpenSettings();

    bool isGpsAvailable();
}

Android implementation

[assembly: Dependency(typeof(LocationShare))]
namespace Projectname.Droid.Services 
{
    public class LocationShare : ILocSettings
    {
        public bool isGpsAvailable()
        {
            bool value = false;
            Android.Locations.LocationManager manager = (Android.Locations.LocationManager)Android.App.Application.Context.GetSystemService(Android.Content.Context.LocationService);
            if (!manager.IsProviderEnabled(Android.Locations.LocationManager.GpsProvider))
            {
                //gps disable
                value = false;
            }
            else
            {
                //Gps enable
                value = true;
            }
            return value;
        }

        public void OpenSettings()
        {
            Intent intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocat‌​ionSourceSettings);
            intent.AddFlags(ActivityFlags.NewTask);
            Android.App.Application.Context.StartActivity(intent);
        }
    }
}

Finally from the shared project called like below:

//For checking the GPS Status
bool gpsStatus = DependencyService.Get<ILocSettings>().isGpsAvailable();
//For opening the location settings 
DependencyService.Get<ILocSettings>().OpenSettings();

For ios how I can I do the same features? I tried like below:

[assembly: Dependency(typeof(LocationShare))]
namespace Projectname.iOS.Serivces
{
    class LocationShare : ILocSettings
    {
        public bool isGpsAvailable()
        {
            //how to check the GPS is on or off here
        }

        public void OpenSettings()
        {
            UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
        }
    }
}

Location settings page opening on ios simulators, but don't know how to check the GPS status.

Best Answer

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Try

    public bool isGpsAvailable()
            {
                if (CLLocationManager.LocationServicesEnabled)
                {
                    var status = CLLocationManager.Status;
                    if (status == CLAuthorizationStatus.AuthorizedAlways
                        || status == CLAuthorizationStatus.Authorized
                        || status == CLAuthorizationStatus.AuthorizedWhenInUse)
                    {
                        return true;
                    }
    
                    return false;
                }
    
                return false;
            }
    

    Refer to

    https://stackoverflow.com/a/34862296/8187800

  • SreeeeSreeee INMember ✭✭✭✭✭

    @ColeX I have tried the CLLocationManager code and it is not working as expected. It returns true always even if the location is off or on.

    OpenSettings() function code (UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));) is also not working as expected, it is redirecting to some other page, I need to open the location settings page if the GPS is off.

    Also, I am requesting location permission like below:

    var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
    

    In android, location permission is asking, but in ios, no permissions are asking.

  • ColeXColeX Member, Xamarin Team Xamurai

    Did you add Permission for location?

    Add the following keys in info.plist

    NSLocationWhenInUseUsageDescription
    NSLocationAlwaysAndWhenInUseUsageDescription
    NSLocationAlwaysUsageDescription
    
    

    Check https://developer.apple.com/documentation/corelocation/requesting_authorization_for_location_services?language=objc .

  • SreeeeSreeee INMember ✭✭✭✭✭

    @ColeX Yes, I have added location permissions in info.plist file, here is the screenshot. But the location permissions are not asking. I have tried Permissions.LocationWhenInUse instead of Permissions.LocationAlways. Always getting false value for GPS status and able to load the app settings page instead of device location settings page.

    Is it possible to load the device location settings page in ios? Somewhere I saw, iOS can set the GPS switch separately for apps.

  • ColeXColeX Member, Xamarin Team Xamurai
    edited November 2020

    Could you post the code of checking permissions ? Here is a case similar to yours .

    Is it possible to load the device location settings page in ios?

    We can use App-Prefs:root=LOCATION_SERVICES to open location page for iOS 10 and below (actually it's not recommended ,because prefs URLs are considered as private APIs) .

    However After iOS 10 apple only allows us to open the root setting page , we can't navigate to exact setting page .

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited November 2020

    @ColeX Following is my code for checking permission:

    //Requesting permission like below:
    var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
    if (status == PermissionStatus.Granted)
    {
        //Then checking the GPS state like below
        bool gpsStatus = DependencyService.Get<ILocSettings>().isGpsAvailable();
        if (!gpsStatus)
        {
            //show a message to user here for sharing the GPS
            //If user granted GPS Sharing, opening the location settings page like below:
            DependencyService.Get<ILocSettings>().OpenSettings();
        }
    }
    

    And my iPhone version is 14.2 :)

    I have added all the permissions on info.plist like below:

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited November 2020

    I have tried the below 2 codes for requesting or checking permission. In both cases, the status value is Disabled. If I uninstall the app and reinstall it again, getting the same status and not showing any permission pop-up window.

    var status = await Permissions.RequestAsync<Permissions.LocationAlways>();
    var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
    

    I go through the SO thread you have shared, I have added 4 permissions on info.plist. Is that affect the permission pop-up window? Don't know why the permission is not asking even a single time?

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer
    1. Check permission before Request , https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=ios

    2. Add the following three keys into info.plist : https://github.com/Baseflow/flutter-geolocator/issues/172#issuecomment-498990408 .

    NSLocationAlwaysUsageDescription
    
    NSLocationWhenInUseUsageDescription
    
    NSLocationAlwaysAndWhenInUseUsageDescription
    
    1. Test on a real device .
  • sisaackssisaacks Member ✭✭✭

    Hello, I am following along with your posts.....is there anyway on IOS to get notified when the user turns locations services on/off? For android i have a broadcaster. Basically if the user tunrs on/off location services on IOS I need to let the user know they cannot do certain things.

Sign In or Register to comment.