1

I am new to Xamarin iOS and mobile dev in general. I have a application which requires location services, on my view controller I have a button which takes the user to location settings for the app, however, if the main device location is off the user will not be able to do anything with the app level location setting.

I am using this code on my button click event to take the user to the settings page.

if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
    NSString settingsString = UIApplication.OpenSettingsUrlString;
    NSUrl url = new NSUrl(settingsString);
    UIApplication.SharedApplication.OpenUrl(url);
}

I would like to know if there is a way to check if device level Location services are off and take the user to that settings page instead of app-level location settings and vice versa.

Also how to take users to Location settings screen if device level location services is disabled. I tried a few combinations but I am unsure what the NSUrl will be.

Abe
  • 1,879
  • 2
  • 24
  • 39

3 Answers3

2

To check the Device level location permission:

 bool deviceLevel = CLLocationManager.LocationServicesEnabled;

Document here: determining_the_availability_of_location_services

To check the app level location permission:

public void CheckAuthorization(CLLocationManager manager, CLAuthorizationStatus status)
{

    switch (status)
    {
        case CLAuthorizationStatus.Authorized | CLAuthorizationStatus.AuthorizedAlways | CLAuthorizationStatus.AuthorizedWhenInUse:
            Console.WriteLine("Access");
            break;
        case CLAuthorizationStatus.Denied | CLAuthorizationStatus.NotDetermined | CLAuthorizationStatus.Restricted:
            Console.WriteLine("No Access");
            break;
        default:
            Console.WriteLine("No Access");
            break;
    }
}

Document here: clauthorizationstatus

Update:

Have a look at answers in there two threads: how-to-programmatically-open-settings-privacy-location-services-in-ios-11 and how-to-open-location-services-screen-from-setting-screen

There says

Avoid use of "prefs:root" or "App-Prefs:root" in you app, otherwise App will be rejected from App Store. Just open Setting page.

You can not open the device location permission directly, it is not allow through App Store rules.

Just use UIApplication.OpenSettingsUrlString; to open the setting page.

nevermore
  • 15,432
  • 1
  • 12
  • 30
  • Can you help me with how to show users the Location services setting when deviceLevel = false. I have updated my question with this also. Cheers. – Abe Jun 20 '19 at 13:09
  • @Abe See my updated answer. Don't forget to mark/upvote it if this answer is helpful to you. – nevermore Jun 21 '19 at 02:00
  • @ Jack Hua - MSFT - Thanks for you help. Yes I have but realised it is not worth using perf roots as the app might be rejected from app store so just showing a message to the user if the top level location is disabled based on the flag as per your answer. Thanks again. – Abe Jun 26 '19 at 08:00
1

Welcome to mobile and Xamarin! Yes there are several Nuget Packages you can add that will help you do this. One that's gaining popularity is Xamarin Essentials.

As they show in the documentation, just try to get the location, it will handle permissions by itself, and if you face PermissionException, then you can open the settings as you are! Happy Coding

GiampaoloGabba
  • 1,428
  • 1
  • 15
  • 21
Saamer
  • 4,687
  • 1
  • 13
  • 55
  • @Sameer - Thanks for your comment. I have looked into using James Montemagno's Plugins.Permissions but this is a bit of overkill for the mobile app i am working on atm. Thank you though! – Abe Jun 20 '19 at 13:07
1

You can check if the user has disabled the Location services at the settings level then check the app level:

if(!CLLocationManager.LocationServicesEnabled)
{
    Console.WriteLine("Location Services are off globally go to settings");
    // This may get your app rejected using the strings below
    if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
    {
        UIApplication.SharedApplication.OpenUrl(new NSUrl("App-Prefs:root=General"));
    }
    else
    {
        UIApplication.SharedApplication.OpenUrl(new NSUrl("prefs:root=General"));
    }
}
else if (CLLocationManager.Status == CLAuthorizationStatus.Denied ||
         CLLocationManager.Status == CLAuthorizationStatus.NotDetermined ||
         CLLocationManager.Status == CLAuthorizationStatus.Restricted)
{
    Console.WriteLine("Location Services are off just for your app, got to app settings");
    UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
}

In terms of opening to the system settings or the app settings, UIApplication.OpenSettingsUrlString will go to the app settings as per the docs:

UIApplicationOpenSettingsURLString Used to create a URL that you can pass to the openURL: method. When you open the URL built from this string, the system launches the Settings app and displays the app’s custom settings, if it has any.

You can use the string:

prefs:root=General

or for iOS 10 and above

App-Prefs:root=General

But Apple may reject your app, tbh I think it's not worth trying to go to the settings just for this reason but up to you.

Iain Smith
  • 9,230
  • 4
  • 50
  • 61