3

Is there a way to programmatically present the WebKit inspector for a WKWebView? I am aware of how to enable the inspector for a WKWebView but what I would like to do is, similar to what is described here for a WebView, to be able to also directly pop it up on the screen programmatically from my own code. Use of private APIs and other hacks are naturally OK for me to accomplish, as this is only for debugging purposes.

The use case I have for this in my debugging flow is a WKWebView in my desktop based app where the right-click events that would usually allow for a context menu to be used for something else app specific.

As a workaround, would be happy with any means not involving right-clicking in the web view itself to bring up the inspector.

Community
  • 1
  • 1
mz2
  • 4,672
  • 1
  • 27
  • 47
  • Perhaps you could click through safari's menu with apple script: http://apple.stackexchange.com/a/118144 – Onato May 17 '16 at 22:11
  • Unless I'm mistaken that would only help me to get the inspector up for the iOS simulator? I wasn't aware there's a way to open up the inspector from Safari to a Mac app? – mz2 May 17 '16 at 22:14

1 Answers1

4

Reclaiming the right-click, eh? Interesting.

I had to plumb a large portion of WKWebKit2 C API headers into my swift app's build.

Roughly, to pull up the JS console:

WKInspectorShowConsole(WKPageGetInspector((wkwebview.subviews.first as! WKView).pageRef))

The gory details: https://github.com/kfix/MacPin/commit/5fed6cb01ab88170f1387122748f8c4ae9e2cab5#diff-c5ffd49d5bf2cde5e056621f9ab14545R426

You need to pull in WKBase.h and WKView+Privates.h

kfix
  • 628
  • 4
  • 12