禁用对位置服务API的访问

本文关键字:API 访问 服务 位置 | 更新日期: 2023-09-27 18:12:37

如何禁用访问Location Service API ?

我确实收到了一封来自微软开发中心的信,里面有这样的提示:

你的应用必须提供允许用户启用的应用内设置禁用你的应用访问和使用的位置从位置服务API .

谁能提供进一步的帮助,我如何去做这件事?

禁用对位置服务API的访问

将此代码粘贴在MainPage.xaml中的InitializeComponent();之后。您必须通过这一行using System.IO.IsolatedStorage;添加对IsolatedStorage的引用。

if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
{
    return;
}
else
{
    MessageBoxResult result = MessageBox.Show("Allow this app to access your location?", "Location", MessageBoxButton.OKCancel);
    if (result == MessageBoxResult.OK)
    {
       IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
    }
    else
    {
       IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
    }
    IsolatedStorageSettings.ApplicationSettings.Save();
}

也创建一个设置。xaml页面,带有ToggleSwitch,其代码如下:

if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
{
    if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] == true)
    {
       locationSwitch.IsChecked = true;
    }
    else
    {
       locationSwitch.IsChecked = false;
    }
}
else
{
    MessageBoxResult result = MessageBox.Show("Allow this app to access your location?", "Location", MessageBoxButton.OKCancel);
    if (result == MessageBoxResult.OK)
    {
       IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
    }
    else
    {
       IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
    }
    IsolatedStorageSettings.ApplicationSettings.Save();
}
private void locationSwitch_Checked(object sender, RoutedEventArgs e)
{
    if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
    {
       IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
       IsolatedStorageSettings.ApplicationSettings.Save();
    }
}
private void locationSwitch_Unchecked(object sender, RoutedEventArgs e)
{
   if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
   {
      IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
      IsolatedStorageSettings.ApplicationSettings.Save();
   }
}

在使用位置/GPS数据的页面上包括以下代码:

if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] == true)
{
     //Do Something
}
else
{
     MessageBox.Show("Please enable location services to use this feature. You can turn it on from Settings.");
}

这肯定会有帮助。我用相同的。如果这对你也有帮助,请投票并标记为答案:)

你的应用程序使用位置服务,你需要有能力禁用它或你问一般?

如果是第一个,那么停止收集数据并在应用程序中禁用它。如果是第二个,那么进入WPmanifest并取消勾选