iOS - Html/JS/CSS/IMG content stored in Documents don't run on WKWebView

LucasFrancoLucasFranco USMember ✭✭
edited October 2016 in Xamarin.iOS

I'm developing an application that downloads some files on a web server and stores it in the directory Documents on the iOS platform. I do this because my application needs to work offline at certain times and downloaded files can be upgraded at any time.

The files that I download from the server are: html, javascript, css ... The structure down to the mobile looks like this:

- assets
   - html
      - service.html
      - team.html
   - css
      - style.css
   - js
      - javascript.js
   - img
      - logo.png
      - icon.png

So I call the LoadHtmlString method with baseURL. Therefore, when the html is rendered via WKWebView, images and other elements, such as js and css are not processed.

When I run the application with the files stored on NSBundle (Content of the application directory) all features work perfectly. I'm making a mistake, or the iOS platform does not support this? Otherwise, what is the correct way to proceed?

Thank you so much!

Best regards,

Lucas Schmidt

Posts

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @LucasFranco,

    Can you upload a small sample project?

    I'm confused exactly how the path looks here for you. Are you first getting the path to the documents folder to use for the BaseUrl?

    var documents =
     Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments); 
    var filename = Path.Combine (documents, "assets/html/service.txt");
    
  • LarryOBrienLarryOBrien USXamarin Team Xamurai

    Are you passing a correct base URL in your call to UIWebView.LoadHTMLString(string,NSURL)? For instance:

    var html = File.ReadAllText("main.html");
    var nsurl = NSUrl.CreateFileUrl(new [] { NSBundle.MainBundle.BundlePath });
    myWebView.LoadHtmlString(html, nsurl); 
    

    (Obviously, changing file names and path!)

    Additionally, I've found that the most reliable way to "kickstart" JavaScript in an embedded UIWebView is to add an explicit call in the LoadingFinished method of the UIWebViewDelegate:

    class MyWebViewDelegate : UIWebViewDelegate 
    {
        public override void LoadingFinished(UIWebView webView)
        {
           var onLoadResult = webView.EvaluateJavaScript("OnLoad();"); 
        }
    } 
    
  • LucasFrancoLucasFranco USMember ✭✭

    My html content is stored in the path: /var/mobile/Containers/Data/Application/65FB2E9C-29AC-4B4D-8CA7-6F93F6D3A2B9/Documents/assets/

    The files are found correctly by File.Exists ()

    The value I'm using the BasePath () is: "file:///var/mobile/Containers/Data/Application/65FB2E9C-29AC-4B4D-8CA7-6F93F6D3A2B9/Documents/assets/"

    But the image is not loaded!

    Remembering that I'm downloading my html content (dynamically) and storing this path: "/var/mobile/Containers/Data/Application/65FB2E9C-29AC-4B4D-8CA7-6F93F6D3A2B9/Documents/assets/"

    I'm not working with files within the project, via BundleResources!!!

  • LucasFrancoLucasFranco USMember ✭✭

    It works correctly when I put my files in the tmp directory rather than Documents, but do not know why. Have any suggestions?

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    You should not be storing a fully qualified path to the files. You should be using one of the Environment.SpecialFolder values as the base. The location will change from one deployment to another, so you can't store the fully-qualified path.

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @LucasFranco,

    Can you share the entire code where you are downloading and loading the files?

  • LucasFrancoLucasFranco USMember ✭✭

    I do not think so. I imagine that the error is a bug, which was described in the forum below and described a possible solution to my problem:

    https://github.com/XLabs/Xamarin-Forms-Labs/issues/1157#issuecomment-254595522

  • DaveHuntDaveHunt USMember ✭✭✭✭✭

    Shouldn't you be using LoadFileUrl to navigate to a local file anyway?

  • BalaAKBalaAK INMember ✭✭

    @LucasFranco ..Any update Regarding this issue...

Sign In or Register to comment.