0

I'm new to Xamarin apps and I'm trying to show a pdf file in an iOS app. I read that it's possible to achieve that by using a webview. I tried to load a pdf file from document folder into a webview, but I just got a blank page.

What I have tried

I. I followed this Microsoft tuturial, where it's told that webview can read local files from embedded resources. I created a custom webview and a custom renderer to manage the Uri, as written in this post:

namespace App.Controls
{
    public class CustomWebView : WebView
    {
        public static readonly BindableProperty UriProperty 
            = BindableProperty.Create(propertyName: "Uri",
                returnType: typeof(string),
                declaringType: typeof(CustomWebView),
                defaultValue: default(string));

        public string Uri
        {
            get { return (string)GetValue(UriProperty); }
            set { SetValue(UriProperty, value); }
        }

    }
}
[assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]
namespace App.iOS.Renderers
{
    public class CustomWebViewRenderer : ViewRenderer<CustomWebView, UIWebView>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                SetNativeControl(new UIWebView());
            }
            if (e.OldElement != null)
            {
                // Cleanup
            }
            if (e.NewElement != null)
            {
                Control.ScalesPageToFit = true;
            }
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            var customWebView = Element as CustomWebView;
            if (string.IsNullOrEmpty(customWebView.Uri)) return;
            Control.LoadRequest(new NSUrlRequest(NSUrl.FromFilename(customWebView.Uri)));
            Control.ScalesPageToFit = true;
        }
    }

I put a pdf file into Resources folder and the file was actually shown:

protected async override void OnAppearing()
{
     // ...
     webViewPdf.Uri = "10065.pdf";
}

II. What I actually need is to dynamically download pdf files from a network folder to a local cache and to show them. I can't use the Resource folder as local cache, because this folder is only accessible for reading.

So I changed my code in this way:

protected async override void OnAppearing()
{
    string localPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
    string fileName = "10065.pdf";
    string fullName = Path.Combine(localPath, fileName);
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

        var pdfDownloader = new Services.Synchronization.PdfDownloader(client);
        await pdfDownloader.DownloadFile(fileName, localPath); // downloads the file and saves it into localPath
    }
    if (File.Exists(fullName))
    {
        //webViewPdf.Source = fullName;
        //webViewPdf.Source = @"file://" + fullName;
        //webViewPdf.Uri = @"file://" + fullName;
        webViewPdf.Uri = fullName; 
    }
}

As you can see, I tried to assign the file path to the webview in different ways, but I always get a blank page. I'm not sure if I'm writing the file path in the wrong way or if it's not possible to show files that are located out of the Resources folder.

Alessandro
  • 116
  • 6

0 Answers0