Forum Xamarin.iOS

Set a local file path to a webView Source

SimonDelliseSimonDellise BEMember ✭✭

Hi,

I'm trying to display a PDF saved in local on my iOS App.
Here's my code, it works with a Web URL (example : http://xamarin.com) but not with a local url (example : /Users/username/Library/Developer/CoreSimulator/Devices/5FRA9CFD-CEVC-445D-B9C4-3D6708A469V2/data/Containers/Data/Application/9E851B1A-9DA3-43CE-9103-1DE7ED86E434/Documents/myFile.pdf).

Here's my C# code :

public class WebViewDoc : ContentPage
{
    public WebViewDoc (String filename)
    {
        String documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
        String fullPath = Path.Combine (documentsPath, filename);
            // Result : /Users/username/Library/Developer/CoreSimulator/Devices/5FRA9CFD-CEVC-445D-B9C4-3D6708A469V2/data/Containers/Data/Application/9E851B1A-9DA3-43CE-9103-1DE7ED86E434/Documents/myFile.pdf

        var browser = new WebView ();
        browser.Source = @fullPath; // Works if replacing by a web url
        Content = browser;

    }
}

Did someone have a solution ?

Thank you,
Simon

Posts

  • JasonAwbreyJasonAwbrey USInsider, University, Developer Group Leader mod

    have you tried using a "file://" scheme with the url?

  • SimonDelliseSimonDellise BEMember ✭✭

    Hi @JasonAwbrey ,

    Yes, and still not ok. :neutral:

    Here's the code to save the file on my app, maybe the problems come from it?

    public async void SaveFile (String filename, byte[] fileByteArray)
    {
        var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
        var path = Path.Combine (documents, filename); // tried to replace documents by "file://" here and in the code above.
        File.WriteAllBytes (path, fileByteArray);
    }
    
  • hvaughanhvaughan USMember ✭✭✭

    @SimonDellise , @JasonAwbrey

    I am currently having the same issue. There are plenty of example for Android but not for iOS. I tried using file:// without success. I am wondering if it is a simulator issue or something else...

  • hvaughanhvaughan USMember ✭✭✭

    @SimonDellise
    Awesome! Thank you. I think that solved it for me. I also was not using .ApplicationData.

    One more question if you don't mind, for Android, when you open using another application (such as adobe reader), are you handling the case when the user does not have any PDF viewer installed? I found PDF.js (link below), but just wanted to see if you had a better alternative. Thanks again.

    http://subodhjena.com/xamarin-displaying-pdf-files-on-android-webview/

  • SimonDelliseSimonDellise BEMember ✭✭

    @hvaughan You're welcome :)

    Unfortunately, I haven't found any better solution at your question. I think PDF.js is a great alternative to the native app.

Sign In or Register to comment.