Monkey D. Luffy
Monkey D. Luffy

Reputation: 321

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 websites 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 obtaining current location on all three platforms (IOS, Android, Windows) through Xamarin.Froms. Android is working perfectly but IOS is where i got stuck. Any type of help is appreciated.

Upvotes: 1

Views: 506

Answers (1)

ColeX
ColeX

Reputation: 14475

After comparing with the official sample , I found the issue was that the timeout parameter you set is too short for the task completion, so it canceled every time.

Solution :

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

Upvotes: 1

Related Questions