iOSHTMLViewer.LoadURL

From Xojo Documentation

Method

iOSHTMLViewer.LoadURL(url As Text)

Supported on Mobile(iOS).

Displays the specified URL. Be sure to include the appropriate prefix, such as "http://".

Notes

To load HTML from a file on the device, pass FolderItem.URLPath to LoadURL.

To load HTML from Text, first save it to a file using TextOutputStream and then load it using the URLPath to the FolderItem.

Sample Code

Display wikipedia:

HTMLViewer1.LoadURL("https://www.wikipedia.org")

To display HTML that is in a file, you can load it by using the URLPath for the file. This example saves HTML to a text file and then loads the file into an HTMLViewer:

Var html As Text = "<html><body>Hello!</body></html>"

Var htmlFile As FolderItem = SpecialFolder.Documents.Child("hello.html")
Var output As TextOutputStream
output = TextOutputStream.Create(htmlFile, TextEncoding.UTF8)
output.WriteLine(html)
output.Close

HTMLViewer1.LoadURL(htmlFile.URLPath)

Note: The above code may not load the file due to sandboxing. If that happens to you, try the Loading HTML From Documents Folder code below.

Loading Directly from Text

You can use this Declare to load HTML directly from Text:

Var html As Text = "<html><body>Hello!</body></html>"

Declare Sub loadHTML Lib "UIKit.Framework" Selector "loadHTMLString:baseURL:" (obj_id As Ptr, html As CFStringRef, url As Ptr)
loadHTML(HTMLViewer1.Handle, html, Nil)

Loading HTML From Documents Folder

iOS sandboxing of the app and the HTML Viewer can prevent the HTML Viewer from seeing files that are in the app's Documents folder. This code can be used to provide access to files in the Documents folder:

Declare Function NSClassFromString Lib "Foundation.framework" (clsName As CFStringRef) As ptr
Declare Function URLWithString Lib "Foundation.framework" Selector "URLWithString:" ( id As Ptr, URLString As CFStringRef ) As Ptr

Declare Sub loadFileURL Lib "UIKit.framework" Selector "loadFileURL:allowingReadAccessToURL:" (obj As Ptr, url As Ptr, readAccessURL As Ptr)

// htmlFile is a Xojo.IO.FolderItem from Xojo.IO.SpecialFolder.Documents
Var nsURL As Ptr = URLWithString(NSClassFromString("NSURL"), htmlFile.URLPath)
Var readURL As Ptr = URLWithString(NSClassFromString("NSURL"), htmlFile.parent.URLPath)

loadFileURL(HTMLViewer1.Handle, nsURL, readURL) // Display the contents in an iOSHTMLViewer