32

I'm looking for a way to show the inspector for a WKWebView inside my Mac app.

With WebKit1 and WebView it was easy to show the inspector inside your Mac app, by just setting WebKitDeveloperExtras to true in your UserDefaults. That would give you an "Inspect Element" menu in every web view.

But in WebKit2 with WKWebView this is not working anymore. In the WWDC14 inspector session they explain that you have to add an entitlement and can then show the inspector from the Safari developer menu. This only works if you are the developer.

I looked through the private headers and found _allowsRemoteInspection which makes me think you can somehow launch an inspector and connect to it, but I'm not sure where to go from there.

Although I'm hoping for an official way to do this, my app is not in the AppStore, so I'm ok with using private stuff.

Koen Bok
  • 3,234
  • 3
  • 29
  • 42
  • Although I don't have an answer to your question, I do know what the _allowsRemoteInspection is likely to do. If you open Safari and go to the Develop menu, you'll see a number of devices (iPhone, etc) that allow you to remotely debug webpages on a device or simulator. So I don't think this is the property that you're looking for to provide in-app inspector behaviour. – Jamie Chapman Aug 15 '14 at 13:45
  • The other workaround is to create an Applescript to launch Safari browser with Developer menu and package this script within the app. When needed you can invoke this script from your App. – Nirbhay Kundan Aug 16 '14 at 05:09
  • 6
    I need to the same thing for iOS. – GuidoMB Aug 25 '14 at 19:01
  • @GuidoMB did you ever find a solution? – Aaron Ash May 08 '17 at 16:37

5 Answers5

32

For Swift, instead of building a bridging header you can set it directly

self.webView.configuration.preferences.setValue(true, forKey: "developerExtrasEnabled")
Drazen
  • 2,776
  • 1
  • 25
  • 39
  • 3
    that is like steak on the grill mmmmmm – quemeful Dec 01 '16 at 13:58
  • I submitted a Feedback Assistant report about making this functionality officially supported: https://github.com/feedback-assistant/reports/issues/80 Please duplicate that report if you wanna see this happen. – Sindre Sorhus Jan 17 '20 at 07:03
18

Based on what Koen found, an easier way to set this property is to use Key Value Coding, no bridging headers required.

Swift:

let preferences = webView.configuration.preferences
preferences.setValue(true, forKey: "developerExtrasEnabled")

Or in Objective-C:

[webView.configuration.preferences setValue:@YES forKey:@"developerExtrasEnabled"];

Key Value Coding will look for methods and instance variables that match the key, including private ones prefixed by an underscore.

Patrick Smith
  • 518
  • 6
  • 9
6

This was patched here: https://lists.webkit.org/pipermail/webkit-dev/2014-August/026790.html

Just expose the private property like this and you can use it.

@interface WKPreferences (WKPrivate)
@property (nonatomic, setter=_setDeveloperExtrasEnabled:) BOOL _developerExtrasEnabled;
@end

Now you get the "Inspect Element" menu on right click.

The only thing I still need to find out is how to show the inspector directly from code.

Koen Bok
  • 3,234
  • 3
  • 29
  • 42
2

isInspectable

Starting on macOS Ventura (13.3), there is a new property isInspectable on WKWebView; turning it on will enable the inspector. No need to rely on private APIs or KVO anymore.

wkWebView.isInspectable = true

Warning

There is a caveat tho, to show the inspector, you need to right-click on a website, and the context menu should have the option Inspect Element. Sometimes this is not the case, and you need to open the Web Inspector from Safari (developer tools need to be enabled on Safari).

Safari > Develop > [Name of your device] > [Name of your app] > [URL or title of website open with WKWebView]
vicegax
  • 4,709
  • 28
  • 37
1

Building on Koen Bok's answer, for Swift, confer this gist. Using those files, you'll need to add the following line to your bridging header:

#import "WKPreferences+DevExtras.h"

Usage looks like

let webView = WKWebView(frame: window.frame)
webView.configuration.preferences.enableDevExtras();
Clay Bridges
  • 11,602
  • 10
  • 68
  • 118
  • I used this approach in my Swift project, but I get `-[WKPreferences enableDevExtras]: unrecognized selector sent to instance 0x610000006d40` and the webView is staying empty. Any ideas? – danielbuechele Apr 05 '15 at 13:35