首页 > 解决方案 > Xamarin iOS 上的位置权限问题

问题描述

问题是当您启动应用程序时,会出现一个允许定位的窗口,并且下面的逻辑停止运行。用户必须在启用后再次单击该按钮,然后执行代码中的逻辑。

当用户单击“允许一次”或“在使用应用程序时始终允许”时如何执行此操作,无需再次按下按钮即可执行逻辑。

我的代码:

async Task ButtonClickedGPS()
    {
        var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();

        if (status == PermissionStatus.Denied && status == PermissionStatus.Disabled)
        {
            _ = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
        }
        else
        {
            try
            {
                var location = await Geolocation.GetLastKnownLocationAsync();

                if (location != null)
                {
                    WeatherBindingData weatherBindingData = await _restServiceForecast.GetWeatherDataForecast(GenerateRequestUriGPS(Constants.OpenWeatherMapEndpoint, location), GenerateRequestUriForecast(ConstantsForecast.OpenWeatherMapEndpoint2, _cityEntry), GenerateRequestUriAir(ConstantsAir.OpenWeatherMapEndpoint3, location));
                   
                    _cityEntry.Text = CityName;

                    await DisplayHourlyForecast();
                }
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                // Handle not supported on device exception
            }
            catch (FeatureNotEnabledException fneEx)
            {
                // Handle not enabled on device exception
            }
            catch (PermissionException pEx)
            {
                // Handle permission exception
            }
            catch (Exception ex)
            {
                // Unable to get location
            }
        }

在我的 info.plist 我添加

Privacy - Location When In Use Usage Description
Privacy - Location Always Usage Description
Privacy - Location Always and When In Use Usage Description

但结果是一样的;(

有没有办法解决这个问题?

标签: c#iosxamarinmobilelocation

解决方案


推荐阅读