HTMLViever.LoadPage wrong error

This is a new one, I use this project with this code since… I started my MBP m1 last year. This specific code have not been changed recently and was working some days ago.

Xojo 2022r4.1 (and earlier) / Ventura 13.2

The error Message (and reason, same string) is:
The BaseFile parametThe BaseFile parameter to LoadPage must be an existing file, not folder.
This is against what the LR said (both are valmid arg).
But, I do not care, I passed a file instead of a folder to get the same result.

The offending code is:


wRender.HTMLViewer1.LoadPage(TA.Text, Data_FI)

I System.DebugLog the passed FolderItem (Data_FI) (NativePath) to be sure of what it Reference and, yes, I get the folder path or the file path…

What have I done (as of this night):
→ Close the project and Quit Xojo,
→ Clear the Cache folder contents,
→ Reboot,
→ Disk Utils (internal SSD, no external nor cloud, SMB anything)
→ Power Off the laptop,
→ Boot, fire Xojo (as the only application), load and run the project to get the same Exception.

I saved the html generated code from this Desktop application and load it with Safari without trouble.

Now, here we are: I cannot render the html code anymore…

ideas ?

Incredible:

I checked online the documentation and found:


Var f As FolderItem = FolderItem.TemporaryFile
HTMLViewer1.LoadPage(TextArea1.Text, f)

I use that and the error goes away.

I use Xojo 2021r2.1, and the syntax I used until yesterday worked fine (passing a specific folder, not a file).

I do not understand how this can be.

For unknow reasons, the HTMLviewer does not show the images, while the manually saved html (loaded in Firefox) shows them (even Quick Look Finder’s display them !).

I’m cursed !

I concur. Indeed HTMLViewer does not seem to display picture files.

LoadPage(Source As String, RelativeTo As FolderItem)

Loads the the supplied Source into the HTMLViewer. RelativeTo must be an actual file and not a folder.


Any links will be resolved relative to the passed RelativeTo FolderItem.

Resources/myhtml/basefile.html
Resources/myhtml/myimage.png

Var f As FolderItem = SpecialFolder.Resources.Child("myhtml").Child("basefile.html")
HTMLViewer1.LoadPage(myhtmlString, f)

Does it work?

What images? Where are they stored?

There are 2 possible issues with local images. Trying to show them stored in places in relative paths (even if declared as an absolute path) needing going backwards the root RelativeTo file, or… for macs, needs some entitlement to gain such loading access.

That’s why I was asking @Emile_Schwarz where they are stored. If they are local, you need to call a method to get permission (on macOS) to do so. This method, which uses Declares, was kindly provided to me by @Sam_Rowlands so he would need to authorise my passing it on.

The solution is to save html to a subfolder of specialfolder.applicationdata, and to copy the pictures there as well. Then instead of using LoadPage, use LoadURL.

Permission granted.

2 Likes

Thank you all for your answers. I left internet soon after my last message (above).

In the heart of the night, I fired Xojo 15r1 and checked my files on my i5 mbp (check for file/folder privileges). It worked fine (excepted the project version was old…).

Minutes ago, and with fresh look, I fired Xojo 2021r2.1 and checked my code. In fact, I commented what worked without images yesterday, and tried to implement a different code until I realized where the code lies.

This seems to be an error (some may say a bug) I’ve done. Sometimes the brain (like computer) fall into error mode…

My project is now back at work. What I do not understand is how I could mis-match two variables… And I do that yesterday (I’ve made a fast check with a yesterday early morning version). May the France strike do something here ?

BTW: the code shared in the LR works fine for an html without data (images, css, whatever), but who, nowaday, have such html files ?
The passed FolderItem is located in the same folder the images folder resides and that was good / the html knows where the images are.

Remember: the saved - with a different method / button - worked fine, so I knew from the start the html was not in fault.

What said William that started with “All’s well…” ? :grinning:

1 Like

I’ve read about the CEF security problem. LoadPage(file) can be used. It will work.

The problem is that as local images in the filesystem must be loaded using the file protocol, the html code must be loaded from the file system too.

LoadPage(string, baseFile) wont work.
It doesn’t obey such rule, the code origin was the data: protocol, it probably could load data: resources but not file: ones.

Then it is better to use LoadURL.

If Xojo give some reason or deprecate LoadPage(fileFolderItem), could be. If not, using the call designed by Xojo for such, probably is enough.

LoadPage works as far as page code is concerned. The issue comes when pointing to images. Even with a full path, it does not show.

Hope @TimStreater soon posts Sam’s declare.

Oh, that’s another issue. LoadPage(htmlCode, folderItem) causes this too. LoadPage(folderItem) fix. Due to CEF security. The macOS entitlement issue needing Sam’s hack to override it is another issue.

This is Sam’s declare method:

#if targetMacOS and xojoVersion >= 2020.01 then
  
  // --- Originally created in July 2020. <--- Leave this info here so it's easier to track which version of the code.
  //     Published Sep 1st 2020.
  //     written by Sam Rowlands of Ohanaware.com
  //     Apple documentation for this API: https://developer.apple.com/documentation/webkit/wkwebview/1414973-loadfileurl?language=objc
  
  declare function NSClassFromString               lib "Foundation" (inClassName as CFStringRef) as Integer
  declare Function NSURLfileURLWithPathIsDirectory lib "Foundation" selector "fileURLWithPath:isDirectory:" (NSURLClass as Integer, path as CFStringRef, directory as Boolean) as Integer
  declare function WKWebViewloadFileURL            lib "WebKit" selector "loadFileURL:allowingReadAccessToURL:" (Viewer as Ptr, URL as Integer, readAccessURL as Integer) as Integer
  
  // --- Create a NSURL object from a Xojo Folderitem.
  Var folderURL as Integer = NSURLfileURLWithPathIsDirectory (NSClassFromString ("NSURL"), inFolder.nativePath, inFolder.IsFolder)
  
  // --- This bit is not technically correct. The first parameter after the instance should actually be the page that you're trying to load.
  //     But as we're not loading a page per se... For the purpose of just setting access rights, we ignore the return
  //     value as we don't need to display progress.
  
  Call WKWebViewloadFileURL (h.handle, folderURL, folderURL)
  
#EndIf

.
Method signature is:
.

grantAccessToFolder (extends h as DesktopHTMLViewer, inFolder as folderItem)

I use this as follows (htmlbody has the html string, myViewer is the DesktopHTMLViewer in question, and ff is a folderitem pointing at the folder containing the images):

Var  myViewer as DesktopHTMLViewer, htmlbody as String, ff as FolderItem
myViewer.grantAccesstoFolder (ff)
myViewer.LoadPage (htmlbody, ff)

I’m pretty sure I use file:// in the tags, I’ll have to check that but right now I’m being paged for supper.

Interestingly, I’ve made a test here, and it worked in the Mac without any issues and without hacks.

Follows the test and source

https://drive.google.com/file/d/1_8Jy7k8ZBIVyz5i9hmd--ABD3ukJHyMe/view?usp=sharing

For Windows, only the LoadPage(FolderItem) works, LoadPage(String, FolderItem) doesn’t.

For Mac, both works.

Xojo 22r4.1

Tested on macOS 11.5.2

Simpler than that, in fact. I just do this:

<img src="someimage.jpg">

but if I don’t run Sam’s declare method then the image is not loaded - comes up as a small square with a blue ? inside it.

Check my sample. Check if it runs there like here.

Do I need a newer macOS machine to break it?