SwiftUI provides more than just UIHostingController / _UIHostingView (iOS) and NSHostingController / NSHostingView (macOS) for embedding SwiftUI views into UIKit/AppKit.
Starting from iOS 18.0 / macOS 15.0, SwiftUI also offers CAHostingLayer<Content: View> — a CALayer subclass that can host SwiftUI content directly at the layer level. This API is currently marked as SPI (@_spi(ForUIKitOnly) / @_spi(ForAppKitOnly)) and is used internally by UIKit, AppKit, and WebKit.
Since CAHostingLayer is an SPI, it's not available through public SwiftUI headers. Apple's WebKit project uses a clever technique: a .swiftinterface file with -module-abi-name SwiftUI that re-exports the SPI symbols under a separate module name, linking to the real SwiftUI framework at runtime.
-
Place the
SwiftUI_SPI.swiftinterfacefile in your project (e.g.Modules/Platform/cocoa/). -
Add the directory to your build settings:
SWIFT_INCLUDE_PATHS = $(inherited) $(SRCROOT)/Modules/Platform/cocoa -
Import the module:
import SwiftUI_SPI
import SwiftUI_SPI
@available(iOS 18.0, macOS 15.0, *)
func makeHostingLayer() -> CALayer {
let layer = CAHostingLayer(rootView: Color.red)
layer.bounds = CGRect(origin: .zero, size: CGSize(width: 200, height: 200))
return layer
}- WebKit — SwiftUI_SPI.swiftinterface
- WebKit — WKMaterialHostingSupport.swift (example of
import SwiftUI_SPIusage)