question

SreejithSree-2948 avatar image
1 Vote"
SreejithSree-2948 asked JarvanZhang-MSFT commented

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

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.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
1 Vote"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

To check if the location service is enabled on iOS , try using the CLLocationManager.LocationServicesEnabled method.

bool status = CLLocationManager.LocationServicesEnabled;
//check the status's value

Tutorial:
https://docs.microsoft.com/en-us/dotnet/api/corelocation.cllocationmanager.locationservicesenabled?view=xamarin-ios-sdk-12



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

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.


0 Votes 0 ·
jcmanke avatar image jcmanke SreejithSree-2948 ·

On iOS, you only get to ask once. If the user denies the permission, you just have to bother them to go to the app settings and grant it there.

0 Votes 0 ·

The location permission is not asking even a single time. I have tried Permissions.LocationWhenInUse instead of Permissions.LocationAlways, but no luck.

0 Votes 0 ·
Show more comments