Xamarin iOS 如何检查定位服务是否已关闭或应用程序级设备位置是否已关闭 [英] Xamarin iOS how to check if Location Services is off or app level device location is off

查看:35
本文介绍了Xamarin iOS 如何检查定位服务是否已关闭或应用程序级设备位置是否已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Xamarin iOS 和移动开发的新手.我有一个需要定位服务的应用程序,在我的视图控制器上,我有一个按钮可以将用户带到应用程序的位置设置,但是,如果主设备位置关闭,用户将无法对应用程序级别执行任何操作位置设置.

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.

还有如何在设备级定位服务被禁用的情况下将用户带到定位设置屏幕.我尝试了几种组合,但我不确定 NSUrl 会是什么.

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.

推荐答案

要检查设备级别的位置权限:

To check the Device level location permission:

 bool deviceLevel = CLLocationManager.LocationServicesEnabled;

此处的文档: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;
    }
}

此处的文档:clauthorizationstatus

更新:

看看有两个线程的答案:how-to-programmatically-open-settings-privacy-location-services-in-ios-11how-to-open-location-services-screen-from-setting-screen

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

那里说

避免在您的应用中使用prefs:root"或App-Prefs:root",否则App 将被 App Store 拒绝.只需打开设置页面.

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

不能直接打开设备位置权限,App Store规则不允许.

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

只需使用UIApplication.OpenSettingsUrlString;打开设置页面.

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

这篇关于Xamarin iOS 如何检查定位服务是否已关闭或应用程序级设备位置是否已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆