xamarin仅适用于iOS插件。Geolocator授权错误

时间:2019-02-23 16:44:56

标签: xamarin xamarin.forms

嗨,我有一些代码可以获取它在andproid中起作用但在iOS中不起作用的地理位置。

仅iOS 中使用Plugin.Geolocator我得到

  

位置信息权限被拒绝,无法获得位置信息异步。未处理   异常:Plugin.Geolocator.Abstractions.GeolocationException:A   发生地理位置错误:未经授权

以下是我在plist中的内容(已删除不相关的内容)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        etc.......

        <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
        <string>This app needs access to location when open and in the background.</string>
        <key>NSLocationAlwaysUsageDescription</key>
        <string>Can we use your location</string>
        <key>NSLocationWhenInUseUsageDescription</key>
        <string>We are using your location</string>
        <key>RequestWhenInUseAuthorization</key>
        <string>Need location for geolocator plugin</string>
    </dict>
</plist>

它适用于android,所以我想不是代码而是设置。

有什么建议吗?

编辑 尝试了插件本身附带的示例,即使它似乎可以工作 我在输出窗口中看到错误。我需要在模拟器中设置一些设置吗?怎么样?

  

GeolocatorSampleiOS [16172:675663091]拒绝位置权限,可以   没有获得异步位置。

我在android和iOS上都使用的代码(适用于android)

private async Task<Location> GetDeviceLocationAsync2()
{
    try
    {
        var locator = CrossGeolocator.Current;
        var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
        if (status != PermissionStatus.Granted)
        {
            var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
            if (results.ContainsKey(Permission.Location))
            {
                status = results[Permission.Location];
            }
        }

        var currentLocation = await Xamarin.Essentials.Geolocation.GetLastKnownLocationAsync(); 

        if (currentLocation != null)
        {
            return currentLocation;
        }

        if (!locator.IsGeolocationAvailable || !locator.IsGeolocationEnabled)
        {
            //todo
            return null;
        }

        if (status == PermissionStatus.Granted)
        {
            var request = new GeolocationRequest(
                GeolocationAccuracy.Best, 
                new TimeSpan(30000))
            {
                DesiredAccuracy = GeolocationAccuracy.Best
            };

            currentLocation =await Xamarin.Essentials.Geolocation.GetLocationAsync(request);
            LogCurrentLocation(currentLocation);
        }
        else if (status != PermissionStatus.Unknown)
        {
            await DisplayDialogAsync("Location denied", "Do not have access to location");
        }

        return currentLocation;
    }
    catch (FeatureNotSupportedException ex)
    {
        // Handle not supported on device exception
        //todo:add logging
    }
    catch (PermissionException ex)
    {
        // Handle permission exception
        //todo:add logging
    }
    catch (Exception ex)
    {
        // Unable to get location
        //todo:add logging
    }
    return null;
}

1 个答案:

答案 0 :(得分:0)

已解决。 我需要在模拟器中设置权限!!!