Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1900 from Adam-Langley/main
Browse files Browse the repository at this point in the history
Fix Share API does not correctly block when awaited
  • Loading branch information
jfversluis committed May 4, 2022
2 parents 31f3de2 + 4066392 commit 29ad28d
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions Xamarin.Essentials/Share/Share.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ namespace Xamarin.Essentials
{
public static partial class Share
{
static Task PlatformRequestAsync(ShareTextRequest request)
static async Task PlatformRequestAsync(ShareTextRequest request)
{
var src = new TaskCompletionSource<bool>();
var items = new List<NSObject>();
if (!string.IsNullOrWhiteSpace(request.Text))
{
Expand All @@ -21,7 +22,13 @@ static Task PlatformRequestAsync(ShareTextRequest request)
items.Add(new ShareActivityItemSource(NSUrl.FromString(request.Uri), request.Title));
}

var activityController = new UIActivityViewController(items.ToArray(), null);
var activityController = new UIActivityViewController(items.ToArray(), null)
{
CompletionWithItemsHandler = (a, b, c, d) =>
{
src.TrySetResult(true);
}
};

var vc = Platform.GetCurrentViewController();

Expand All @@ -33,11 +40,14 @@ static Task PlatformRequestAsync(ShareTextRequest request)
activityController.PopoverPresentationController.SourceRect = request.PresentationSourceBounds.ToPlatformRectangle();
}

return vc.PresentViewControllerAsync(activityController, true);
await vc.PresentViewControllerAsync(activityController, true);
await src.Task;
}

static Task PlatformRequestAsync(ShareMultipleFilesRequest request)
static async Task PlatformRequestAsync(ShareMultipleFilesRequest request)
{
var src = new TaskCompletionSource<bool>();

var items = new List<NSObject>();

var hasTitel = !string.IsNullOrWhiteSpace(request.Title);
Expand All @@ -50,7 +60,13 @@ static Task PlatformRequestAsync(ShareMultipleFilesRequest request)
items.Add(fileUrl); // No title specified
}

var activityController = new UIActivityViewController(items.ToArray(), null);
var activityController = new UIActivityViewController(items.ToArray(), null)
{
CompletionWithItemsHandler = (a, b, c, d) =>
{
src.TrySetResult(true);
}
};

var vc = Platform.GetCurrentViewController();

Expand All @@ -62,7 +78,8 @@ static Task PlatformRequestAsync(ShareMultipleFilesRequest request)
activityController.PopoverPresentationController.SourceRect = request.PresentationSourceBounds.ToPlatformRectangle();
}

return vc.PresentViewControllerAsync(activityController, true);
await vc.PresentViewControllerAsync(activityController, true);
await src.Task;
}
}

Expand Down

0 comments on commit 29ad28d

Please sign in to comment.