Accessing WKNavigationAction.sourceFrame.request crashes

Hi all,

I'm currently working with WKWebView and implementing the WKNavigationDelegate protocol. In particular, I'm trying to inspect the sourceFrame of a WKNavigationAction to make navigation policy decisions based on the frame's URL path.

Here's the relevant Swift code inside decidePolicyFor:

public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
    // ...
    let sourceFrame: WKFrameInfo = navigationAction.sourceFrame
    let request: URLRequest = sourceFrame.request // <- SIGABRT occurs here
    // ...
}

The issue is that the app crashes with a SIGABRT at runtime when attempting to access sourceFrame.request. According to Swift's type system, neither sourceFrame nor its request property are optional, so at first glance this seems safe. However, the crash report suggests otherwise.

From the crash log, it appears that the issue arises during the bridging from Objective-C to Swift:

Thread 1 Queue : com.apple.main-thread (serial)
#0	0x00000001a127a030 in static Foundation.URLRequest._unconditionallyBridgeFromObjectiveC(Swift.Optional<__C.NSURLRequest>) -> Foundation.URLRequest ()
#1	0x00000001056c48b0 in CustomWebViewController.webView(_:decidePolicyFor:preferences:decisionHandler:)
#2	0x00000001056c4c78 in @objc CustomWebViewController.webView(_:decidePolicyFor:preferences:decisionHandler:) ()
#3	0x00000001b8c66e0c in WebKit::NavigationState::NavigationClient::decidePolicyForNavigationAction ()
#4	0x00000001b8fd14dc in WebKit::WebPageProxy::decidePolicyForNavigationAction ()
#5	0x00000001b8fcfc7c in WebKit::WebPageProxy::decidePolicyForNavigationActionAsyncShared ()
#6	0x00000001b8fcfb18 in WebKit::WebPageProxy::decidePolicyForNavigationActionAsync ()
#7	0x00000001b87ddaa0 in WebKit::WebPageProxy::didReceiveMessage ()
#8	0x00000001b869f474 in IPC::MessageReceiverMap::dispatchMessage ()
#9	0x00000001b878dda4 in WebKit::WebProcessProxy::dispatchMessage ()
#10	0x00000001b878d614 in WebKit::WebProcessProxy::didReceiveMessage ()
#11	0x00000001b869e7e4 in IPC::Connection::dispatchMessage ()
#12	0x00000001b869e358 in IPC::Connection::dispatchIncomingMessages ()
#13	0x00000001b9a96a44 in WTF::RunLoop::performWork ()
#14	0x00000001b9a96688 in WTF::RunLoop::performWork ()
#15	0x00000001a2428b9c in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#16	0x00000001a24289b4 in __CFRunLoopDoSource0 ()
#17	0x00000001a2428810 in __CFRunLoopDoSources0 ()
#18	0x00000001a2429190 in __CFRunLoopRun ()
#19	0x00000001a242ad4c in CFRunLoopRunSpecific ()
#20	0x00000001ef705454 in GSEventRunModal ()
#21	0x00000001a4e45890 in -[UIApplication _run] ()
#22	0x00000001a4e10cec in UIApplicationMain ()
#23	0x00000001a4ef261c in ___lldb_unnamed_symbol275689 ()
#24	0x00000001059a5104 in static UIApplicationDelegate.main() ()
#25	0x00000001059a5074 in static AppDelegate.$main() ()
#26	0x00000001059a82ec in main ()
#27	0x00000001c940af0c in start ()

This implies that while Swift treats sourceFrame.request as non-optional, the underlying Objective-C implementation may actually return nil—leading to a crash when the non-optional Swift type attempts to force unwrap it.

My question: Is there a way to safely access navigationAction.sourceFrame.request —- or determine if it’s nil—before Swift attempts the implicit bridging from Objective-C? Or is there an established workaround for safely inspecting this property?

Any guidance or best practices for avoiding this crash would be greatly appreciated!

Thanks in advance.

Answered by DTS Engineer in 836332022

Our engineering teams need to investigate this issue, as resolution may involve changes to Apple's software. Please file a bug report, include a small Xcode project and some directions that can be used to reproduce the problem, and post the Feedback number here once you do. If you post the Feedback number here I'll check the status next time I do a sweep of forums posts where I've suggested bug reports.

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

Our engineering teams need to investigate this issue, as resolution may involve changes to Apple's software. Please file a bug report, include a small Xcode project and some directions that can be used to reproduce the problem, and post the Feedback number here once you do. If you post the Feedback number here I'll check the status next time I do a sweep of forums posts where I've suggested bug reports.

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

I have filed a issue within the feedback assistant tool: https://feedbackassistant.apple.com/feedback/17308484

Thank you for filing a bug report. I verified your bug report has been routed to the correct team and it is under investigation.

Accessing WKNavigationAction.sourceFrame.request crashes
 
 
Q