Jay Lieske
Jay Lieske

Reputation: 4858

How can I use Safari to debug a WKWebView in my Mac app?

I'm developing a Mac app that uses WKWebView to display HTML resources. I want to use the Safari inspector to debug the JavaScript in my app.

When I debug my app by launching it from Xcode, Safari doesn't show my app in the Develop menu — it displays "No Inspectable Applications".

How do I get my Mac app to appear in Safari's Develop menu?

Upvotes: 6

Views: 6020

Answers (4)

Serge Ivamov
Serge Ivamov

Reputation: 364

for macOS apps:

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

and right click on WebView for "Inspect Element" button.

Upvotes: 0

Greg Frame
Greg Frame

Reputation: 111

One very important change to all this at least as of 2023 (iOS 16.4) is you have to set your wkwebview to inspectable.

self.theWebview.inspectable = YES;

Upvotes: 7

Prafull
Prafull

Reputation: 591

Download & try same in Safari Technology Preview

https://developer.apple.com/safari/technology-preview/

Upvotes: 0

Jay Lieske
Jay Lieske

Reputation: 4858

From https://forums.developer.apple.com/thread/21521:

Add an entitlement file to the application, then set the entitlement key com.apple.security.get-task-allow to YES. This key is automatically added to iOS applications for Xcode debugging, but it must be added manually to Mac applications.

If you only want to use Safari to inspect while Xcode is running a debug build, add the entitlement file to a Debug configuration and not the Release configuration.

Upvotes: 4

Related Questions