ios - Xamarin Forms 位置服务适用于 iOSimulator,但不适用于 Testflight 构建
问题描述
我正在开发一个 Xamarin Forms 应用程序,该应用程序使用PermissionsPlugin向用户询问位置服务权限。我在 Windows 中使用 Visual Studio 2017 并使用虚拟机作为我的 MAC 构建主机(macOS High Sierra 10.13.4)。
我的问题是应用程序在部署到 iOS 模拟器时按预期工作。它提示用户允许或拒绝使用位置服务。在模拟器中,该应用程序甚至列在“设置”>“隐私”>“位置服务”下。但是在将构建的 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 中;不幸的是,这两种情况都没有提示权限请求对话框。
作为参考,这在 Android 中在 Play 商店的调试和测试中都可以正常工作。
解决方案
我得到了修复;只需要包括所有 3 NSLocationWhenInUseUsageDescription
, NSLocationAlwaysUsageDescription
, 和NSLocationWhenInUseUsageDescription
...
我的印象是我只能添加NSLocationWhenInUseUsageDescription
和中的一个NSLocationAlwaysUsageDescription
。我一定在某个地方读过一个过时的答案,给我留下了错误的印象。
推荐阅读
- php - 对于动态 PHP 变量名,$$foo 与 ${$foo} 有什么区别?
- java - libgdx 构建不断失败导致 LWJGL
- node.js - Docker 监听端口 3000,但浏览器上没有显示
- javascript - 使用开始时间和结束时间将对象数组拆分为一小时跨度组?
- azure - Cosmos DB 客户端日志记录
- r - R中矩阵运算的不合格数组限制
- html - Bootstrap float-right 不适用于我想在右侧对齐的 div
- arrays - 两个字符数组一一获取带有空格的输入
- python - 如何根据python列表中重复元素的索引创建新的python列表
- javascript - 使用 ASP.NET Core 将 Sweet Alert 重定向到 URL(默认确定按钮)