Grisha Grisha - 1 month ago 8
C# Question

Initiate a phone call and return to app afterwards - Xamarin

In my Xamarin application, after user clicks some button I want to make a phone call and then return to my app (ideally to the same place in the code).

Something like this:

async Task button_click()
{
string s = "200-345-6789";
// await Device.OpenUri("tel:" + s); // can't await this
ShowAlert("The number " + s + " was called");
}


Originally I tried Device.OpenUri, but it's not awaitable.

On Android, StartActivityWithResult worked well for me. I define TaskCompletionSource, start activity with an intent of ActionCall, and inside OnActivityResult function I flag my task as completed, which returns the flow back to the original function.

But on iOS, I can't find anything similar. What would you suggest?

Answer

After messing with this for some more hours, I found the solution - so I thought I'd post it for others to benefit. What I did is simulation of something similar to Android's OnActivityResult. Inside my AppDelegate class I define an event which is fired from its OnActivated function. Now, inside my function I subscribe to this event passing to the delegate a TaskCompletionSource which I define. Then I call Device.OpenUri("tel:" + phoneNum). The phone call is made, and after it ends the user is returned to my app, AppDelegate.OnActivated fires my event, my delegate is called and I can mark my task as completed by calling SetResult. This makes possible to await this function.