我正在使用Xamarin Forms应用程序,该应用程序使用PermissionsPlugin向用户询问位置服务权限。我使用Windows中的Visual Studio 2017和虚拟机作为我的MAC构建主机(macOS High Sierra 10.13.4)。
我的问题是,当部署到iOSimulator时,应用程序按预期工作。它会提示用户允许或拒绝使用位置服务。在模拟器中,该应用甚至列在设置>隐私>位置服务下。但在将构建的IPA文件提供给Apple并通过Testflight进行测试后,该应用程序不会提示任何内容,只是无限期地加载。该应用也未在设置>隐私>位置服务中列出。
以下是应用程序中用于请求权限的代码:
async Task<bool> AskPermission()
{
var hasPermission = false;
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
{
await App.Current.MainPage.DisplayAlert("Need location", "Allow app to access location?", "OK", "Cancel");
}
var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
if (results.ContainsKey(Permission.Location))
{
status = results[Permission.Location];
}
}
if (status == PermissionStatus.Granted)
{
hasPermission = true;
}
else if (status != PermissionStatus.Unknown)
{
await App.Current.MainPage.DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
}
}
catch (Exception ex)
{
}
return hasPermission;
}
这是单击按钮时调用的方法之一。然后会出现一个活动指示器,因为它绑定到在启动/完成所有方法时切换的布尔值。
这几乎是James Montemagno插件的示例代码。我怀疑应用程序被卡在await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location)
上。
我还在App的iOS项目中添加了对Info.plist的必要补充:
<key>NSLocationWhenInUseUsageDescription</key>
<string>prompt message</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>prompt message</string>
我还测试了使用NSLocationAlwaysUsageDescription
代替NSLocationWhenInUseUsageDescription
的变体,但得到了类似的结果......
我在iOSimulator中测试了这个(iPhone 5,6,7,全部在11.3,全部都有效)并在Testflight中使用iPhone 7和iPad进行了测试,两者都在11.3中;不幸的是,两个案例都没有提示权限请求对话框。
作为参考,在Play商店的调试和测试中,这适用于Android。
答案 0 :(得分:0)
我得到了修复;只需要包含所有3个NSLocationWhenInUseUsageDescription
,NSLocationAlwaysUsageDescription
和NSLocationWhenInUseUsageDescription
...
我的印象是我只能添加NSLocationWhenInUseUsageDescription
和NSLocationAlwaysUsageDescription
中的任何一个。我必须在某个地方读过一个过时的答案给我留下了错误的印象。