Embedding self-built WebKit framework in Mac app

I'm trying to embed a self-built copy of the WebKit frameworks to a macOS app. Most importantly I hope to get some features to work which Safari offers, but WKWebView in macOS doesn't (getDisplayMedia, Service Workers, WebInspector).

Many years ago I was successful in using a self-built WebKit copy in this Mac app, but it seems the WebKit framework got more complex since them, I guess because of WKWebView's architecture.

That time I had to open the projects for the main frameworks in Xcode, select the framework bundle in the target and change the "Installation Directory" setting to the path @executable_path/../Frameworks. After building WebKit using the build script, I could use otool -L to confirm the changed installation path, which then was displayed for example as @executable_path/../Frameworks/WebCore.framework/Versions/A/WebCore

I tried the same with a current WebKit build:

  1. I copied the products for WebKit.framework, WebCore.framework, JavaScriptCore.framework, WebKitLegacy.framework, WebGPU.framework and WebInspectorUI.framework to my app and added it to the "Frameworks, Libraries and Embedded Content" section in the Project's Target/General tab and selected "Embed & Sign" for each framework.
  2. In "Build Phases" I made sure that WebCore.framework and WebGPU.framework are only in the "Copy Files" phase (Destination Frameworks) and not in "Link Binary with Libraries", as WebCore is linked through the WebKit umbrella framework and WebGPU gave another error (not sure about how to deal with that framework, as in the system it's in a PrivateFrameworks subfolder).
  3. In "Build Settings" I made sure that @executable_path/../Frameworks is entered for "Runpath Search Paths" (it was already probably because of Cocoapods, together with @loader_path/../Frameworks.
  4. When I build my app, the system's WebKit version is used. Only when I add the environment variable DYLD_FRAMEWORK_PATH with value @executable_path/../Frameworks in the run scheme, the embedded self-build WebKit frameworks are used.
  5. Because of currently necessary backward compatibility my app can use the legacy WebView or WKWebView. The legacy WebView works perfectly with the embedded WebKitLegacy.framework. But if I try to open any URL in WKWebView, no content is rendered and in the console output I can see:

Safe Exam Browser[21391:145678] [Process] 0x10c67d760 - [PID=0] WebProcessProxy::didFinishLaunching: Invalid connection identifier (web process failed to launch)

Safe Exam Browser[21391:145678] [Process] 0x10c67d760 - [PID=0] WebProcessProxy::processDidTerminateOrFailedToLaunch: reason=4

Safe Exam Browser[21391:145678] [ProcessSuspension] 0x10c005040 - [PID=0, throttler=0x10c67d8d8] ProcessThrottler::Activity::invalidate: Ending background activity / 'WebProcess initialization'

Safe Exam Browser[21391:145678] [Process] 0x10c67d760 - [PID=0] WebProcessProxy::shutDown:

Safe Exam Browser[21391:145678] [Process] 0x7fbe89064020 - [pageProxyID=40, webPageID=41, PID=0] WebPageProxy::processDidTerminate: (pid 0), reason 4

2022-02-14 12:53:01.764074+0100 Safe Exam Browser[21391:145678] [Process] 0x10c67d760 - [PID=0] WebProcessProxy::processTerminated:

Safe Exam Browser[21391:145678] [Loading] 0x7fbe89064020 - [pageProxyID=40, webPageID=41, PID=0] WebPageProxy::dispatchProcessDidTerminate: reason=Crash

Safe Exam Browser[21391:146842] [SEBOSXWKWebViewController webViewWebContentProcessDidTerminate:<Safe_Exam_Browser.SEBOSXWKWebView: 0x7fbe88f8b1c0>]

I have the impression that the web process might fail to launch because I didn't embed all necessary parts of the self-built WebKit (the product folder contains a large number of XPC, dylib and .a files). Or some additional paths have to be adjusted before building WebKit, so that the embedded frameworks/libraries are used and not the system provided ones. I also looked at the bundle of the Safari Technology Preview and can see some similarities but also differences.

I would be grateful if anybody could provide me with information how to embed a self-built copy of WebKit into a macOS app. Unfortunately I didn't find any Mac open source browser using an embedded copy of WebKit to get some inspiration from.

Answered by DTS Engineer in 873578022

I recommend filing feedback reports about any of the reasons you have for requiring your own compiled version of the WebKit framework in your app. This is useful information for our engineering teams and information we want to know about.

Bug Reporting: How and Why? has tips on creating your bug report.

If you intend to use an alternative web browsing engine in your app(s) and you intend to distribute your app through the App Store, please be aware of App Store Guideline 2.5.6 (that you can find on this page).

Before copying any Apple built and owned frameworks from a system software installation into your projects please contact Apple Legal (https://www.apple.com/legal/) and obtain a license allowing you to do so.

In Signing & Capabilities, in the App Sandbox section => Network, tick the checkbox next to "Outgoing Connections (Client)"

Any luck with this?

No, unfortunately not. I asked an Apple WebKit engineer and I interpreted the vague answer that they have some security features build into WKWebView, which basically prevent you to run a self-built copy.

I was able to solve my initial problem of using getDisplayMedia() in older macOS versions (it's officially supported in macOS Ventura) with a private WebKit API. Service Workers actually work in WKWebView since a while. I didn't figure out how to use WebInspector directly in a WKWebView, you need to run your app in Xcode and connect to it using the Develop menu in Safari.

I am in a somewhat similar situation, except that in my case I want my app to use an older (Apple official) WebKit framework version instead of most recent available because there has been some change in WebKit behaviour since macOS 26.2 which causes errors during Webkit javascript communications. Therefore I am trying to embed a copy of Apple's WebKit.framework from an earlier macOS version. However, XCode build results in a signing error with "bundle format unrecognized, invalid, or unsuitable" for the WebKit framework. Is it possible to embed an older WebKit framework version, and if so how to do so in XCode?

I recommend filing feedback reports about any of the reasons you have for requiring your own compiled version of the WebKit framework in your app. This is useful information for our engineering teams and information we want to know about.

Bug Reporting: How and Why? has tips on creating your bug report.

If you intend to use an alternative web browsing engine in your app(s) and you intend to distribute your app through the App Store, please be aware of App Store Guideline 2.5.6 (that you can find on this page).

Before copying any Apple built and owned frameworks from a system software installation into your projects please contact Apple Legal (https://www.apple.com/legal/) and obtain a license allowing you to do so.

Given age of the original post, and the fact that my issue does not relate to a self-build framework. I'm going to submit this as new issue.

Embedding self-built WebKit framework in Mac app
 
 
Q