如果用户拒绝位置权限,则Xam.Plugin.Geolocator iOS应用程序崩溃

时间:2018-03-07 18:20:36

标签: xamarin xamarin.forms xamarin.ios

我目前使用Geolocator Plugin版本4.2.0。

安装应用程序后,将显示以下提示:

enter image description here

但是,如果用户选择不允许,则应用程序崩溃时会出现以下错误:

Plugin.Geolocator.Abstractions.GeolocationException: A geolocation error occured: Unauthorized
  at Plugin.Geolocator.GeolocatorImplementation+<StartListeningAsync>d__33.MoveNext () [0x000eb] in <51c894f634a24ed2b17b19807ba0f99e>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <cf9013b38a4e4129bd64785080dd2844>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <cf9013b38a4e4129bd64785080dd2844>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <cf9013b38a4e4129bd64785080dd2844>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <cf9013b38a4e4129bd64785080dd2844>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () [0x00000] in <cf9013b38a4e4129bd64785080dd2844>:0 
 at Divco.App+<StartListening>d__11.MoveNext () [0x000b5] in <a1469ede5dd148df922c3455ac848705>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <cf9013b38a4e4129bd64785080dd2844>:0 
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in <cf9013b38a4e4129bd64785080dd2844>:0 
  at UIKit.UIKitSynchronizationContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in <b7935acd70e343049845d6fd73e5ec44>:0 
  at Foundation.NSAsyncActionDispatcher.Apply () [0x00000] in <b7935acd70e343049845d6fd73e5ec44>:0 
--- End of stack trace from previous location where exception was thrown ---

重新打开应用程序时,它会继续抛出此错误。

我已经为App.xaml.cs添加了我的代码,看看我是否错过了一篇文章。我按照之前链接的Geolocator插件网站上的教程进行了操作。

App.xaml.cs

    async void StartListening()
    {
        if (CrossGeolocator.Current.IsListening)
            return;

        CrossGeolocator.Current.DesiredAccuracy = 10;

        await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(Settings.PingDurationSeconds), 1, true, new Plugin.Geolocator.Abstractions.ListenerSettings
            {
                AllowBackgroundUpdates = true,
                PauseLocationUpdatesAutomatically = false
            });

        CrossGeolocator.Current.PositionChanged += PositionChanged;
        CrossGeolocator.Current.PositionError += PositionError;
    }

    async void PositionChanged(object sender, PositionEventArgs e)
    {
        //If updating the UI, ensure you invoke on main thread
        var position = e.Position;
        if (!Settings.IsLoggedIn)
            return;
        else if (Settings.IsAuthExpired)
            await loginStore.Refresh();

        var output = "Full: Lat: " + $"{position.Latitude}" + " Long: " + $"{position.Longitude}";
        output += "\n" + $"Time: {position.Timestamp}";
        output += "\n" + $"Heading: {position.Heading}";
        output += "\n" + $"Speed: {position.Speed}";
        output += "\n" + $"Accuracy: {position.Accuracy}";
        output += "\n" + $"Altitude: {position.Altitude}";
        output += "\n" + $"Altitude Accuracy: {position.AltitudeAccuracy}";

        Settings.CurrentPosition = position;

        await dataStore.UpdateLocation(position.Latitude, position.Longitude);

        Debug.WriteLine(output);
    }

    private void PositionError(object sender, PositionErrorEventArgs e)
    {
        Debug.WriteLine(e.Error);
        //Handle event here for errors
    }

    async void StopListening()
    {
        if (!CrossGeolocator.Current.IsListening)
            return;

        await CrossGeolocator.Current.StopListeningAsync();

        CrossGeolocator.Current.PositionChanged -= PositionChanged;
        CrossGeolocator.Current.PositionError -= PositionError;
    }

理想情况下,我想提醒用户他们必须启用位置跟踪才能使用我们的应用程序(因为它是我们服务的主要部分)。

0 个答案:

没有答案