HTMLViever.LoadPage wrong error

Tested with Ventura 13.2 and both work. Mac Mini Intel.

1 Like

Yes, I tested it and both buttons work. But surely the reason is that your folder is within the Resources folder. In my case I was trying to load an image into the HTMLViewer from an arbitrary folder. Before 2020 or so, one just pointed a FolderItem at the folder with the images, and the Viewer loaded any images from it without issue. After that, using Sam’s declare method to have the OS give that folder permission to be loaded from, became mandatory. I think this change happened at some version of Xojo which was using newer Apple APIs.

1 Like

That is exactly what I see.

I don’t understand. What is supposed to go into that method ?

Interesting. I need to do the same as you for htmlbody (using Rick’s sample):

HTMLViewer1.grantAccessToFolder(f)
HTMLViewer1.LoadPage(htmlCode, f)

but if I load a file and set the code as:

HTMLViewer1.grantAccessToFolder(f)
HTMLViewer1.LoadPage(f)

I just get a blank HTMLViewer, if I comment out the first line, then it works. In both tests the declare method makes the macOS to ask for access to the folder.

Edit: to make it clearer, if I use the above 2 lines of code the HTML Viewer is black (I’m using dark mode) and when I click OK turns white:
image

If I remove the first line (the one with grantAccessToFolder) then the HTML Viewer is white and after I click OK then the GIF is shown correctly:

image

Ok, I’ve made another version copying files to a temp folder.

Source: https://drive.google.com/file/d/1cBvw6PKKIqn2sEyK2mZMLS4oVyi97DQ1/view?usp=sharing

Works OK on the Mac as the one in the Resources folder.
No hacks necessary. :man_shrugging:t2:

HtmlViewTest.xojo_binary_project = From resources
HtmlViewTestCopy.xojo_binary_project = From a copy in the Temp folder

But… Not using Ventura.

App Wrapper and Sleep Aid, can’t display any images or CSS from the internal HTML based help (wrote my own help viewer after Monterey wouldn’t display 3rd party application help), without using a declare to display the page, I just get plain HTML.

I believe this is because I’m using the Apple Help Viewer folder structure, where pages are stored in language specific folders, while CSS and images are stored in a central location.

  • en.lproj → index.html
  • shared → styles.css

When I bought this up with Xojo, it was deemed “Not an issue”. Yet, the very fact that people are still sharing a solution I published a while ago, suggests otherwise.

For those of you who’re still using Xojo’s RectControls, you can use this function. It allows you to pass in a root folder of the HTML structure and a specific web page. Feel free to modify this to appease the “2.0 All The Things™” framework if needed. Just make sure if you modify this code, add a little note so people can see by whom and which version of it.

p.s. I don’t handle the result from the WKWebView, which is typically bad practice as this ignores the opportunity to check for errors.

Public Sub loadPageWithAccessToFolder(extends h as HTMLViewer, inPage as folderItem, inRootFoolder 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.
    //     First published Sep 1st 2020.
    //     Updated Dec 9th 2021.
    //     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 NSURL_fileURLWithPathIsDirectory lib "Foundation" selector "fileURLWithPath:isDirectory:"         ( NSURLClass as integer, path as CFStringRef, directory as boolean) as integer
    declare function WKWebView_loadFileURL            lib "WebKit"     selector "loadFileURL:allowingReadAccessToURL:" ( WKWebViewInstance as integer, URL as integer, readAccessURL as integer ) as integer
    
    dim NSURLClass as integer = NSClassFromString( "NSURL" )
    dim pageULR    as integer = NSURL_fileURLWithPathIsDirectory( NSURLClass, inPage.nativePath, inPage.directory )
    dim folderURL  as integer = NSURL_fileURLWithPathIsDirectory( NSURLClass, inRootFoolder.nativePath, inRootFoolder.directory )
    
    // --- Now that we have our page and folder, fire away!
    dim result     as integer = WKWebView_loadFileURL( h.handle, pageULR, folderURL )
    #pragma unused result
    
  #else
    h.loadPage inPage
    
  #EndIf
End Sub

Below is the function for loading HTML from a string, and granting access to a “base” folder.

Public Sub loadHTMLStringWithAccessToFolder(extends h as HTMLViewer, inHTMLContent as string, inRootFoolder as folderItem)
  #if targetMacOS and XojoVersion >= 2020.01 then
    // --- Originally created Dec 9th 2020. <--- Leave this info here so it's easier to track which version of the code.
    //     written by Sam Rowlands of Ohanaware.com
    //     Apple documentation for this API:
    //     https://developer.apple.com/documentation/webkit/wkwebview/1415004-loadhtmlstring?language=objc
    
    declare function NSClassFromString                lib "Foundation"                                         ( inClassName as CFStringRef ) as integer
    declare Function NSURL_fileURLWithPathIsDirectory lib "Foundation" selector "fileURLWithPath:isDirectory:" ( NSURLClass as integer, path as CFStringRef, directory as boolean) as integer
    declare function WKWebView_loadHTMLString         lib "WebKit"     selector "loadHTMLString:baseURL:"      ( WKWebViewInstance as integer, inString as CFStringRef, baseURL as integer ) as integer
    
    // --- Now that we have our page and folder, fire away!
    dim result as integer = WKWebView_loadHTMLString( h.handle, _
      inHTMLContent, _
      NSURL_fileURLWithPathIsDirectory( NSClassFromString( "NSURL" ), _
      inRootFoolder.nativePath, inRootFoolder.directory ) )
    
    #pragma unused result
    
  #else
    h.loadPage inHTMLContent, inRootFoolder.child( 0 )
    
  #EndIf
End Sub

The code that I posted earlier in the same post.

That is what I used since… months:


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

Data_FI is a folder (OK for standard API / Xojo 2021r2.1; don’t remember for 22r’.1).

My data folder (called images) is located there and the path is set to “./images/image_name.png”.

Worked fine since… Sam shared the code to avoid the OS dialog (access to a folder).

My error was to search how to access to the temporary file created/used by the HTMLViever Control to reload it or whatever.

In doing so, I added a FolderItem and mix-match that Variable with Data_FI (above)… Xojo was unable to works correctly with wrong information.

That said, I do not understand the talk between this entry and my previous one.

1 Like

Sorry for being thick, but grantaccesstofolder does not autocomplete, and generates an error.

Likewise, the posted code has several errors.

What gives ?

Are you using the Method in a Module or you added the Method to a Window for example? or just pasting the code as is?

People saw your first 2 posts as that you resolved the problem and are trying to help with your 3rd post (images not showing)

Sub grantAccessToFolder (extends h as DesktopHTMLViewer, 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:" (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

End Sub

I understand now. Thank you, Tim.

1 Like

Image not showing was because I used:

Var f As FolderItem = FolderItem.TemporaryFile

The images folder was not in the same folder where the temporary file resides. Because it was a snippet from the documentation, I do not asked my mind … “does it works ?”. Another error of mine. 't was a real bad day.

Regards.