首页 > 解决方案 > 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 商店的调试和测试中都可以正常工作。

标签: iosxamarin.formspermissionslocationtestflight

解决方案


我得到了修复;只需要包括所有 3 NSLocationWhenInUseUsageDescription, NSLocationAlwaysUsageDescription, 和NSLocationWhenInUseUsageDescription...

我的印象是我只能添加NSLocationWhenInUseUsageDescription和中的一个NSLocationAlwaysUsageDescription。我一定在某个地方读过一个过时的答案,给我留下了错误的印象。


推荐阅读