【问题标题】:Xamarin iOS how to check if Location Services is off or app level device location is offXamarin iOS 如何检查位置服务是否已关闭或应用级设备位置是否已关闭
【发布时间】:2019-11-02 05:55:15
【问题描述】:

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

我在按钮单击事件中使用此代码将用户带到设置页面。

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

我想知道是否有办法检查设备级位置服务是否已关闭并将用户带到该设置页面而不是应用级位置设置,反之亦然。

如果设备级位置服务被禁用,如何将用户带到位置设置屏幕。我尝试了一些组合,但我不确定 NSUrl 会是什么。

【问题讨论】:

    标签: ios xamarin xamarin.ios


    【解决方案1】:

    检查设备级位置权限:

     bool deviceLevel = CLLocationManager.LocationServicesEnabled;
    

    在此处记录:determining_the_availability_of_location_services

    检查应用级位置权限:

    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

    那里说

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

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

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

    【讨论】:

    • 您能否帮助我了解如何在 deviceLevel = false 时向用户显示位置服务设置。我也用这个更新了我的问题。干杯。
    • @Abe 查看我的更新答案。如果此答案对您有帮助,请不要忘记标记/投票。
    • @Jack Hua - MSFT - 感谢您的帮助。是的,但我意识到使用 perf root 是不值得的,因为应用程序可能会被应用程序商店拒绝,因此如果根据您的回答根据标志禁用顶级位置,则只需向用户显示一条消息。再次感谢。
    【解决方案2】:

    欢迎使用移动和 Xamarin!是的,您可以添加几个 Nuget 包来帮助您执行此操作。越来越受欢迎的是 Xamarin Essentials。

    正如他们在documentation 中显示的那样,只需尝试获取位置,它将自行处理权限,如果您遇到 PermissionException,那么您可以按原样打开设置!快乐编码

    【讨论】:

    • @Sameer - 感谢您的评论。我已经研究过使用 James Montemagno 的 Plugins.Permissions 但这对于我正在使用 atm 的移动应用程序来说有点矫枉过正。不过谢谢!
    【解决方案3】:

    您可以检查用户是否在设置级别禁用了定位服务,然后检查应用级别:

    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));
    }
    

    在打开系统设置或应用设置方面,UIApplication.OpenSettingsUrlString会按照文档进入应用设置:

    UIApplicationOpenSettingsURLString 用于创建可以传递给 openURL: 方法的 URL。当您打开使用此字符串构建的 URL 时,系统会启动“设置”应用并显示该应用的自定义设置(如果有)。

    你可以使用字符串:

    prefs:root=General

    或适用于 iOS 10 及更高版本

    App-Prefs:root=General

    但 Apple 可能会拒绝您的应用,我认为仅出于这个原因不值得尝试进入设置,这取决于您。

    【讨论】:

      猜你喜欢
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      相关资源
      最近更新 更多