Xamarin iOS how to check if Location Services is off or app level device location is off

坚强是说给别人听的谎言 提交于 2021-01-01 08:00:07

问题


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.


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/56668570/xamarin-ios-how-to-check-if-location-services-is-off-or-app-level-device-locatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!