WkWebView with URLSchemeHandler


Gerriet M. Denkmann
 

macOS 15.4; Xcode Version 11.4 (11E146)

I have a window with a WkWebView.

This WkWebView should handle a special scheme.
The only way to do this that I know of, is:

@IBOutlet var wkWebView: WKWebView!
guard let webSuperView = wkWebView.superview else ...
let copiedConfiguration = wkWebView.configuration
copiedConfiguration.setURLSchemeHandler(self, forURLScheme: mySpecialScheme )
let newWebView = WKWebView(frame: wkWebView.frame, configuration: copiedConfiguration)
webSuperView.addSubview(newWebView)
wkWebView.removeFromSuperview()
wkWebView = newWebView

The problem: the old wkWebView had (done in IB) several layout constraints.

I tried to copy these constraints to the newWebView; but this crashes with:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION
(code=EXC_I386_INVOP, subcode=0x0)
frame #0: 0x00007fff2fcb5f13 AppKit`-[NSApplication _crashOnException:] + 106
frame #1: 0x00007fff2fa73c1c AppKit`__62+[CATransaction(NSCATransaction)
NS_setFlushesWithDisplayLink]_block_invoke + 805



• Is there a way to configure a URLSchemeHandler in IB?
• Is there a way to add a URLSchemeHandler to an existing WkWebView?

Gerriet.

Join cocoa@apple-dev.groups.io to automatically receive all group messages.