New problem with HTMLViewer.LoadPage since Version 2020 R1

Hi,

I’ve been using the following code without any problems since many versions of Xojo (MacOS):

dim f As FolderItem = GetTemporaryFolderItem
HTMLViewerDetails.LoadPage(App.HTML_sGetFilmdetailsAlsHTML(sFilmIDSel),f)

The function …GetFilmdetails… provides a string with valid HTML. The HTML contains links to local image files, links to youtube videos and of course some formatted text.

All this worked fine up to Xojo Version 2019 R3.2.

After installing Xojo Version 2020 R1 the HTMLViewer doesn’t show the local image any more but only a placeholder for the image. I haven’t changed anything in my code.

Does anyone have an idea?

Thanks!

Eric

Yes, it’s the change of WebKit. Now you have to ask specifically for permission to load local images. The only way to get this is to do some stuff with Declares. We had this on the Testers’ channel, fortunately @Sam Rowlands was able to help me.

@Sam_Rowlands - can I publish the solution you found me for this?

See case <https://xojo.com/issue/60944>

Its’ the same with both WebKit and Native.

Native is webkit on macOS.

Ah, ok. And it’s also the same when I load the images from my public webserver (http).
Is there any solution for using local images in HTMLViewer?

Yes, the one Sam gave me. I had this exact problem during the testing phase of 2020r1. And I’m grateful that he gave it me, as I have no knowledge of interfacing Xojo code with Apple’s APIs (which is what is involved). We’ll have to see if he’s willing either for me to send it you privately or just make it public. But he may feel he wants to add it to his collection of such items, which is only available for purchase. Thing is, that particular collection is at the moment (if I understand correctly) only available as part of a much larger bundle for what for me is lots of money. Have to wait until Sam gets back to us.

I believe that these are two different issues (although I may be incorrect). The one that I have tackled is the inability to load images from disk when passing in HTML code (it works with a HTML file on the volume).

htmlViewer1.grantAccessToFolder( <folderContainingImages> )
htmlViewer1.loadPage( <htmlCodeAsString>, <folderContainingImages> )

For users of the Ohanaware App Kit, this code is already included, for everyone else copy and paste the following code into a shared module.

Public Sub grantAccessToFolder(extends h as HTMLViewer, inFolder as folderItem)
  #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:" ( HTMLViewer as integer, URL as integer, readAccessURL as integer ) as integer
    
    // --- Create a NSURL object from a Xojo Folderitem.
    dim folderURL as integer = NSURLfileURLWithPathIsDirectory( NSClassFromString( "NSURL" ), inFolder.nativePath, inFolder.directory )
    
    // --- 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 say... 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
End Sub

Of course I would love to get paid for my work, “bills to pay, mouths to feed, etc…”

Thank you @TimStreater for requesting permission before sharing this code (or an earlier draft of it). I really do appreciate that you did that.

1 Like

loadFileURL is also in WKWebViewMBS class in MBS Xojo Plugins.

If needed, we can add more classes or methods to our WKWebView classes.