date picker in WKWebView on iOS 17 appear exception: CALayer bounds contains NaN ... _UIDatePickerOverlayPlatterView

Recently many users upgraded to iOS 17 and crashed when selecting a date on a web page, we got the following exception message:

Exception NSException * "CALayer bounds contains NaN: [0 0; 320 nan]. Layer: <CALayer:0x28103d2c0; position = CGPoint (160 190.25); bounds = CGRect (0 0; 0 0); delegate = <_UIDatePickerOverlayPlatterView: 0x135ba5790; frame = (160 190.25; 0 0); transform = [0, 0, 0, 0, 0, 0]; layer = <CALayer: 0x28103d2c0>>; sublayers = (<CALayer: 0x28103de20>, <CALayer: 0x28103cb40>); opaque = YES; transform = CATransform3D (0 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 1)>" 0x0000000281afec70

I'm having the same error with a Cordova app on iOS 17. Weirdly the page that causes the exception doesn't even have a date picker on it.

*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [0 0; 320 nan]. Layer: <CALayer:0x600000305de0; position = CGPoint (160 131); bounds = CGRect (0 0; 0 0); delegate = <_UIDatePickerOverlayPlatterView: 0x10ecc2650; frame = (160 131; 0 0); transform = [0, 0, 0, 0, 0, 0]; layer = <CALayer: 0x600000305de0>>; sublayers = (<CALayer: 0x600000305f60>, <CALayer: 0x6000002fc7e0>); opaque = YES; transform = CATransform3D (0 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 0 1)>'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007ff80048d28d __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007ff800057894 objc_exception_throw + 48
	2   CoreFoundation                      0x00007ff80048d16b -[NSException initWithCoder:] + 0
	3   QuartzCore                          0x00007ff808feb924 -[CALayer setBounds:] + 388
	4   UIKitCore                           0x0000000110d435c5 -[UIView _backing_setBounds:] + 86
	5   UIKitCore                           0x0000000111a7424e -[UIView(Geometry) setBounds:] + 442
	6   UIKitCore                           0x00000001119409d8 -[_UIDatePickerOverlayPresentation _presentNewDatePicker:] + 662
	7   UIKitCore                           0x000000011193f904 __64-[_UIDatePickerOverlayPresentation presentDatePicker:onDismiss:]_block_invoke + 115
	8   UIKitCore                           0x00000001119405d6 __81-[_UIDatePickerOverlayPresentation _prepareDatePickerPresentationWithCompletion:]_block_invoke + 44
	9   UIKitCore                           0x00000001108bbc0f -[UIPresentationController transitionDidFinish:] + 1284
	10  UIKitCore                           0x00000001108c7909 -[_UICurrentContextPresentationController transitionDidFinish:] + 41
	11  UIKitCore                           0x00000001108c0e74 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke.466 + 465
	12  UIKitCore                           0x0000000110a2b089 -[_UIViewControllerTransitionContext completeTransition:] + 101
	13  UIKitCore                           0x0000000111a1799c -[_UIAfterCACommitBlock run] + 55
	14  UIKitCore                           0x0000000111a17e82 -[_UIAfterCACommitQueue flush] + 189
	15  libdispatch.dylib                   0x0000000103b43747 _dispatch_call_block_and_release + 12
	16  libdispatch.dylib                   0x0000000103b449f7 _dispatch_client_callout + 8
	17  libdispatch.dylib                   0x0000000103b54856 _dispatch_main_queue_drain + 1362
	18  libdispatch.dylib                   0x0000000103b542f6 _dispatch_main_queue_callback_4CF + 31
	19  CoreFoundation                      0x00007ff8003e9850 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
	20  CoreFoundation                      0x00007ff8003e418b __CFRunLoopRun + 2463
	21  CoreFoundation                      0x00007ff8003e3409 CFRunLoopRunSpecific + 557
	22  GraphicsServices                    0x00007ff80a650187 GSEventRunModal + 137
	23  UIKitCore                           0x00000001113c43a2 -[UIApplication _run] + 972
	24  UIKitCore                           0x00000001113c8e10 UIApplicationMain + 123
	25  OCA                                 0x00000001030e3e56 main + 54
	26  dyld                                0x00000001034ec3ee start_sim + 10
	27  ???                                 0x00000001045ed41f 0x0 + 4368290847
)
libc++abi: terminating due to uncaught exception of type NSException

I didn't add a date picker to WKWebView either, it was the web developer who evoked the system's date picker. Occasionally it crashes when the user clicks where the date is entered.

Just had another interesting finding. I tried opening the web version of the app (which has mostly the same code) in Mobile Safari and got the same error. So it looks like there's some incompatibility with the Javascript code and WKWebView. (This is from testing on a simulated iPhone 15.)

Found that the cause of the issue was a page-change hook which used jQuery to focus and then blur a date input. When I removed this it no longer crashed.

Apple seems to have fixed the issue on iOS 17.1, and no similar crashes are being monitored on systems above this version anymore.

date picker in WKWebView on iOS 17 appear exception: CALayer bounds contains NaN ... _UIDatePickerOverlayPlatterView
 
 
Q