How to show the inspector within your WKWebView based desktop app?

荒凉一梦 提交于 2019-11-30 04:38:26

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

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

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

Swift:

preferences.setValue(true, forKey: "developerExtrasEnabled")

Or in Objective-C:

[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.

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.

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!