Any chance to "wrap" this native iOS API call into async / await?

async-await, c#, xamarin.ios

Solution

You can use `TaskCompletionSource`:

public Task<bool> DoSomethingAsync()
{
   var taskSource = new TaskCompletionSource<bool>();

   //Call some asynchronous Apple API
   NSSomeAppleApi.DoSomething(error =>
   {
      if (error != null)
         taskSource.SetException(new Exception(error.LocalizationDescription));
      else
         taskSource.SetResult(true);
   });

   return taskSource.Task;
}

Problem

Consider this iOS 6 API call: ``` eventStore.RequestAccess(EKEntityType.Event, (granted, error) => { if(granted) { events = this.GetLocalCalendarEvents(eventStore); }}); ``` I have some code that accesses the calendar and reads events. On iOS5 this just works but on iOS6 I have to ask for access first and, if granted, start reading then. I was wondering if I can somehow wrap this into a combination of async/await to hide the ugly delegate. Any ideas?

Original source