Guilherme Nimer
Guilherme Nimer

Reputation: 555

Xamarin.Essentials Share.RequestAsync() dont working on iOS

i'm having some trouble trying to share some pdfs with xamarin.Essentials on iOS, method is working as expected in Android but when i run in iOS the method is called with no error but nothing happens, Share menu don't open and the page still running with no error, i'll post some code below:

PCL View.xaml (where share button is in)

<ContentPage  xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             BackgroundColor="#6f7d8b" 
             x:Class="SeusPPPs.PDFViewer.View"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             ios:Page.UseSafeArea="true"
             xmlns:controls="clr-namespace:SeusPPPs">
    <ContentPage.Content>
        <Grid  RowSpacing="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="5*"/>
                <RowDefinition Height="95*"/>
            </Grid.RowDefinitions>
            <Label Padding="10,10,10,10" Grid.Row="0" VerticalOptions="Center" Text="COMPARTILHAR" TextColor="White" FontAttributes="Bold" HorizontalOptions="StartAndExpand">
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="SHARE" NumberOfTapsRequired="1"/>
                </Label.GestureRecognizers>
            </Label>
            <Label Padding="10,10,10,10" Grid.Row="0" VerticalOptions="Center" Text="FECHAR" TextColor="White" FontAttributes="Bold" HorizontalOptions="EndAndExpand">
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="Close" NumberOfTapsRequired="1"/>
                </Label.GestureRecognizers>
            </Label>

            <controls:PdfWebView
            x:Name="PdfView" 
            Grid.Row="1"
                             HorizontalOptions="FillAndExpand"
                             VerticalOptions="FillAndExpand" />
        </Grid>
    </ContentPage.Content>

</ContentPage>

code-behind share event

public void SHARE(object sender,EventArgs e)
        {
            if (_filepath == null)
                return;
            try
            {
                Device.BeginInvokeOnMainThread(async () => {
                    await Share.RequestAsync(new ShareFileRequest() {
                         Title = "Compartilhar seu PPP",
                         File = new ShareFile(_filepath)
                    });

                });

            }catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }


        }

Load PDF Method, working fine on both ios and android

private void CarregarPdf(string id)
        {

            var dependency = DependencyService.Get<ILocalFileProvider>();

            if (dependency == null)
            {
                DisplayAlert("Erro ao carregar dependencia", "Dependencia não encontrada", "OK");

                return;
            }

            var localPath = string.Empty;

            string url = "apiurl" + id;

            var fileName = Guid.NewGuid().ToString();

            using (var httpClient = new HttpClient())
            {
                var pdfStream = Task.Run(() => httpClient.GetStreamAsync(url)).Result;
                localPath =
                    Task.Run(() => dependency.SaveFileToDisk(pdfStream, $"{fileName}.pdf")).Result;
            }

            if (string.IsNullOrWhiteSpace(localPath))
            {
                DisplayAlert("Error baixar PDF", "não foi possivel encontrar o arquivo", "OK");

                return;
            }
            _filepath = localPath;
            PdfView.Uri = localPath;
        }

//EDIT

I did notice the problem is only on physical devices, you can reproduce the error using this repository Github

Upvotes: 2

Views: 1837

Answers (1)

Rob L
Rob L

Reputation: 2372

After upgrading to Xamarin 4.8, the problem still existed. But when I added a dummy PresentationSourceBounds, it seems to have fixed the issue?
Note: I had not tried this on earlier versions of Xamarin.

await Share.RequestAsync(new ShareFileRequest
{
  Title = result.Data.Name,
  File = new ShareFile(temporaryFile),
  PresentationSourceBounds = new Rectangle(0, 0, 1, 1) // Fixes iPad share option
});

Upvotes: 3

Related Questions