При запросе разрешения на местоположение приложение падает только в iOS. в ксарине - PullRequest
0 голосов
/ 13 января 2020

enter image description here Я пытаюсь запросить разрешение на местоположение. Когда вызов переходит к requestPermissionAsyn c, происходит сбой всего приложения, я прикрепил нижеприведенный код, пожалуйста go скажите.

var hasPermission = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
if (hasPermission == PermissionStatus.Unknown)
{
    var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
    hasPermission = results[Permission.Location];
}
if (hasPermission == PermissionStatus.Denied)
{
    var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);
    hasPermission = results[Permission.Location];
    if (hasPermission == PermissionStatus.Denied)
    {
        string zipCode = await cache.GetObject<string>(AppConstants.USER_LOCATION_ZIPCODE_KEY);
        settingsService.AddItem(AppConstants.USER_LOCATION_ZIPCODE_KEY, zipCode);
        settingsService.AddItem(AppConstants.USER_LOCATION_LAT_KEY, "");
        settingsService.AddItem(AppConstants.USER_LOCATION_LON_KEY, "");

    }
    else if (hasPermission == PermissionStatus.Granted)
    {
        var locator = CrossGeolocator.Current;
        Plugin.Geolocator.Abstractions.Position currentPosition = await locator.GetPositionAsync(new TimeSpan(0, 0, 0, 10, 0));

        var addressList = await locator.GetAddressesForPositionAsync(currentPosition, null);

        foreach (var item in addressList)
        {
            settingsService.AddItem(AppConstants.USER_LOCATION_ZIPCODE_KEY, item.PostalCode);
            settingsService.AddItem(AppConstants.USER_LOCATION_LAT_KEY, item.Latitude.ToString());
            settingsService.AddItem(AppConstants.USER_LOCATION_LON_KEY, item.Longitude.ToString());
            break;
        }
    }
}

, пожалуйста, помогите мне и спасибо заранее.

StackTrace:

System.Threading.Tasks.TaskCanceledException: A task was canceled.
  at System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x001d4] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.3.0/src/Xamarin.iOS/Foundation/NSUrlSessionHandler.cs:459
  at System.Net.Http.HttpClient.SendAsyncWorker (System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpCompletionOption completionOption, System.Threading.CancellationToken cancellationToken) [0x0009e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:281
  at Qualbe1DMobileApp.Repository.GenericRepository+<>c__DisplayClass3_1`2[T,TR].<PostAsync>b__2 () [0x0003f] in /Users/admin/Desktop/1DentalMobile/1DM_Development/Qualbe1DMobileApp/Qualbe1DMobileApp/Qualbe1DMobileApp/Repository/GenericRepository.cs:147
  at Polly.Policy+<>c__DisplayClass181_0`1[TResult].<ExecuteAsyncInternal>b__0 (Polly.Context ctx, System.Threading.CancellationToken ct) [0x0003d] in C:\projects\polly\src\Polly.Shared\Policy.Async.cs:63
  at Polly.RetrySyntaxAsync+<>c__DisplayClass25_1.<WaitAndRetryAsync>b__1 (Polly.Context ctx, System.Threading.CancellationToken ct) [0x0003d] in C:\projects\polly\src\Polly.Shared\Retry\RetrySyntaxAsync.cs:545
  at Polly.Retry.RetryEngine.ImplementationAsync[TResult] (System.Func`3[T1,T2,TResult] action, Polly.Context context, System.Threading.CancellationToken cancellationToken, System.Collections.Generic.IEnumerable`1[T] shouldRetryExceptionPredicates, System.Collections.Generic.IEnumerable`1[T] shouldRetryResultPredicates, System.Func`1[TResult] policyStateFactory, System.Boolean continueOnCapturedContext) [0x00077] in C:\projects\polly\src\Polly.Shared\Retry\RetryEngineAsync.cs:29
  at Polly.Retry.RetryEngine.ImplementationAsync[TResult] (System.Func`3[T1,T2,TResult] action, Polly.Context context, System.Threading.CancellationToken cancellationToken, System.Collections.Generic.IEnumerable`1[T] shouldRetryExceptionPredicates, System.Collections.Generic.IEnumerable`1[T] shouldRetryResultPredicates, System.Func`1[TResult] policyStateFactory, System.Boolean continueOnCapturedContext) [0x0025d] in C:\projects\polly\src\Polly.Shared\Retry\RetryEngineAsync.cs:50
  at Polly.Policy.ExecuteAsyncInternal[TResult] (System.Func`3[T1,T2,TResult] action, Polly.Context context, System.Threading.CancellationToken cancellationToken, System.Boolean continueOnCapturedContext) [0x000b2] in C:\projects\polly\src\Polly.Shared\Policy.Async.cs:61
  at Polly.Policy.ExecuteAsync[TResult] (System.Func`3[T1,T2,TResult] action, Polly.Context context, System.Threading.CancellationToken cancellationToken, System.Boolean continueOnCapturedContext) [0x00073] in C:\projects\polly\src\Polly.Shared\Policy.Async.ExecuteOverloads.cs:261
  at Qualbe1DMobileApp.Repository.GenericRepository.PostAsync[T,TR] (System.String uri, T data, System.String funcKey, System.String authToken, System.String appId, System.String appKey) [0x004a6] in /Users/admin/Desktop/1DentalMobile/1DM_Development/Qualbe1DMobileApp/Qualbe1DMobileApp/Qualbe1DMobileApp/Repository/GenericRepository.cs:177
  at Qualbe1DMobileApp.Services.Data.MemberService.GetMemberInfo (System.Int64 memberid) [0x00081] in /Users/admin/Desktop/1DentalMobile/1DM_Development/Qualbe1DMobileApp/Qualbe1DMobileApp/Qualbe1DMobileApp/Services/Data/MemberService.cs:34
  at Qualbe1DMobileApp.ViewModels.AccountLandPageViewModel.ShowOptions () [0x00117] in /Users/admin/Desktop/1DentalMobile/1DM_Development/Qualbe1DMobileApp/Qualbe1DMobileApp/Qualbe1DMobileApp/ViewModels/AccountLandPageViewModel.cs:48
  at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
  at Foundation.NSAsyncSynchronizationContextDispatcher.Apply () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.3.0/src/Xamarin.iOS/Foundation/NSAction.cs:178
  at at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.3.0/src/Xamarin.iOS/UIKit/UIApplication.cs:86
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.8.3.0/src/Xamarin.iOS/UIKit/UIApplication.cs:65
  at Qualbe1DMobileApp.iOS.Application.Main (System.String[] args) [0x00001] in /Users/admin/Desktop/1DentalMobile/1DM_Development/Qualbe1DMobileApp/Qualbe1DMobileApp/Qualbe1DMobileApp.iOS/Main.cs:17

Ответы [ 2 ]

1 голос
/ 13 января 2020

Если вы посмотрите на трассировку стека, ваш код завершится неудачно из-за запускаемой вами задачи и будет отменен:

  at Qualbe1DMobileApp.Repository.GenericRepository.PostAsync[T,TR] (System.String uri, T data, System.String funcKey, System.String authToken, System.String appId, System.String appKey) [0x004a6] in /Users/admin/Desktop/1DentalMobile/1DM_Development/Qualbe1DMobileApp/Qualbe1DMobileApp/Qualbe1DMobileApp/Repository/GenericRepository.cs:177
  at Qualbe1DMobileApp.Services.Data.MemberService.GetMemberInfo (System.Int64 memberid) [0x00081] in /Users/admin/Desktop/1DentalMobile/1DM_Development/Qualbe1DMobileApp/Qualbe1DMobileApp/Qualbe1DMobileApp/Services/Data/MemberService.cs:34
  at Qualbe1DMobileApp.ViewModels.AccountLandPageViewModel.ShowOptions () [0x00117] in /Users/admin/Desktop/1DentalMobile/1DM_Development/Qualbe1DMobileApp/Qualbe1DMobileApp/Qualbe1DMobileApp/ViewModels/AccountLandPageViewModel.cs:48

Так что либо MemberService строка 32, либо AccountLandPageViewModel строка 48 должны перехватить и обработать это исключение.

0 голосов
/ 13 января 2020

Спасибо всем, На самом деле я звоню некоторым API, прежде чем спрашивать разрешение на местоположение. поэтому до получения ответа от API дело доходит до разрешения местоположения, поэтому приложение теперь работает нормально. Спасибо за ценное время.

...