macOS HTMLViewer Developer Extras

For anyone that’s using (Desktop)HTMLViewer in their macOS projects that needs the ability to debug the underlying HTML code using the developer extras that are available in Safari, I’ve put together a pair of extension methods below.

Note: These use a Private API which is why they’re blocked behind a DebugBuild compiler directive! Use of this method will cause your app to be rejected in the App Store approval process!

Public Sub EnableDeveloperExtras(Extends ctl as HTMLViewer, assigns value as boolean)
  #if TargetMacOS and DebugBuild
    Declare Function getConfiguration Lib "WebKit" selector "configuration" (obj as integer) As Ptr
    Declare Function getPreferences Lib "WebKit" selector "preferences" (obj as ptr) As Ptr
    Declare sub _setDeveloperExtrasEnabled lib "WebKit" selector "_setDeveloperExtrasEnabled:" (obj as ptr, value as Boolean)
    
    dim config as ptr = getConfiguration(ctl.Handle)
    dim prefs as ptr = getPreferences(config)
    _setDeveloperExtrasEnabled(prefs, value)
  #endif
End Sub
Public Sub EnableDeveloperExtras(Extends ctl as DesktopHTMLViewer, assigns value as Boolean)
  #if TargetMacOS and DebugBuild
    Declare Function getConfiguration Lib "WebKit" selector "configuration" (obj as ptr) As Ptr
    Declare Function getPreferences Lib "WebKit" selector "preferences" (obj as ptr) As Ptr
    Declare sub _setDeveloperExtrasEnabled lib "WebKit" selector "_setDeveloperExtrasEnabled:" (obj as ptr, value as Boolean)
    
    dim config as ptr = getConfiguration(ctl.Handle)
    dim prefs as ptr = getPreferences(config)
    _setDeveloperExtrasEnabled(prefs, value)
  #endif
End Sub
10 Likes

Apparently this code works for iOS as well… In this case you can go to your Mac and access the HTML debugger under the Develop menu in Safari.

1 Like