GetPositionAsync works in android but not in IOS 11

System.Threading.Tasks.TaskCanceledException: A task was canceled.

2017-11-05 14:46:31.228 Project.iOS[36807:4480015] 
Unhandled Exception:
System.Threading.Tasks.TaskCanceledException: A task was canceled.
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00020] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.2.0.11/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:179 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.2.0.11/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156 
  at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.2.0.11/src/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128 
  at System.Runtime.CompilerServices.TaskAwaiter`

I am having hard time in handling the exception thrown by the following code when i try to debug on ios.11 emulator but this code works fine in android.

private async Task<Plugin.Geolocator.Abstractions.Position> GetPositionAsync() {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 50;
            TimeSpan ts = new TimeSpan((Int32)10000);
            _position = await locator.GetPositionAsync(timeout: ts);
            return _position;
        } 

I have followed all the instructions mentioned in various web sights related to @jamesmontemagno /Xamarin.Plugins/Geolocator/ but no luck. I have made sure my info.plist consist of following code:

  <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
  <string>This app needs access location when open and in the background.</string>
  <key>NSLocationAlwaysUsageDescription</key>
  <string>Need location for geolocator plugin.</string>
  <key>RequestWhenInUseAuthorization</key>
  <string>Need location for geolocator plugin.</string>
  <key>NSLocationWhenInUseUsageDescription</key>
  <string>Need location for geolocator plugin.</string> 

All i am trying to achieve is getting current location on all three platforms (IOS, Android, Windows) with current location. Android is working perfectly but IOS is where i got stuck. Any type of help is appreciated.

Tagged:

Best Answer

  • MonkeyD.LuffyMonkeyD.Luffy USMember
    Accepted Answer

    The following code works for me

    await locator.GetPositionAsync(TimeSpan.FromSeconds(10)); 
    

Answers

  • CarlAlbertCarlAlbert USUniversity ✭✭

    I am getting the same error when I try to use GetPositionAsync() for iOS.

    If I try to use the PositionChanged event, it never fires after StartListeningAsync() successfully starts.

  • MonkeyD.LuffyMonkeyD.Luffy USMember
    Accepted Answer

    The following code works for me

    await locator.GetPositionAsync(TimeSpan.FromSeconds(10)); 
    
  • CarlAlbertCarlAlbert USUniversity ✭✭

    If you are using the Xamarin iOS Emulator, I have seen this error there. I have successfully used GetPositionAsync in the iOS 11 simulator on my Mac and also in a built for release project.

Sign In or Register to comment.