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.ActionLocationSourceSettings); 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.
Check permission before Request , https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=ios
Add the following three keys into info.plist : https://github.com/Baseflow/flutter-geolocator/issues/172#issuecomment-498990408 .
NSLocationAlwaysUsageDescription NSLocationWhenInUseUsageDescription NSLocationAlwaysAndWhenInUseUsageDescription
Answers
Try
Refer to
https://stackoverflow.com/a/34862296/8187800
@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:
In android, location permission is asking, but in ios, no permissions are asking.
Did you add Permission for location?
Add the following keys in info.plist
Check https://developer.apple.com/documentation/corelocation/requesting_authorization_for_location_services?language=objc .
@ColeX Yes, I have added location permissions in
info.plist
file, here is the screenshot. But the location permissions are not asking. I have triedPermissions.LocationWhenInUse
instead ofPermissions.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.
Could you post the code of checking permissions ? Here is a case similar to yours .
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 .
@ColeX Following is my code for checking permission:
And my iPhone version is 14.2
I have added all the permissions on
info.plist
like below: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.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?Check permission before Request , https://docs.microsoft.com/en-us/xamarin/essentials/permissions?tabs=ios
Add the following three keys into info.plist : https://github.com/Baseflow/flutter-geolocator/issues/172#issuecomment-498990408 .
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.