Vishal Chandran Vishal Chandran - 2 months ago 41
iOS Question

iOS UIWebView TextField - Application crash on hitting Done button on keyboard after entering text

I have several webviews in my app. Few of the webviews will have a textfield in them.

When running the app initially, on entering some text in the textfield and hitting the 'Done' button on the keyboard causes the app to crash. If I relaunch the app and try the same scenario, it works fine.

I am running few Javascript files as well in the WebView.

This is the error thrown:

EXC_BAD_ACCESS mh_execute_header
Attempted to dereference null pointer.


This is the trace I got from the crash details:

/usr/lib/libobjc.A.dylib:0objc_msgSend
Frameworks/UIKit.framework/UIKit:0-[_UIWebViewScrollViewDelegateForwarder forwardInvocation:]
Frameworks/CoreFoundation.framework/CoreFoundation:0___forwarding___
Frameworks/CoreFoundation.framework/CoreFoundation:0__forwarding_prep_0___
Frameworks/UIKit.framework/UIKit:0-[UIScrollView _getDelegateZoomView]
Frameworks/UIKit.framework/UIKit:0-[UIScrollView _zoomScaleFromPresentationLayer:]
Frameworks/UIKit.framework/UIKit:0-[UIWebDocumentView _zoomedDocumentScale]
Frameworks/UIKit.framework/UIKit:0-[UIWebDocumentView _layoutRectForFixedPositionObjects]
Frameworks/UIKit.framework/UIKit:0-[UIWebDocumentView _updateFixedPositionedObjectsLayoutRectUsingWebThread:synchronize:]
Frameworks/UIKit.framework/UIKit:0-[UIWebDocumentView _updateFixedPositioningObjectsLayoutAfterScroll]
Frameworks/UIKit.framework/UIKit:0-[UIWebBrowserView _updateFixedPositioningObjectsLayoutAfterScroll]
Frameworks/CoreFoundation.framework/CoreFoundation:0__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__
Frameworks/CoreFoundation.framework/CoreFoundation:0_CFXRegistrationPost
Frameworks/CoreFoundation.framework/CoreFoundation:0___CFXNotificationPost_block_invoke
Frameworks/CoreFoundation.framework/CoreFoundation:0-[_CFXNotificationRegistrar find:object:observer:enumerator:]
Frameworks/CoreFoundation.framework/CoreFoundation:0_CFXNotificationPost
Frameworks/Foundation.framework/Foundation:0-[NSNotificationCenter postNotificationName:object:userInfo:]
Frameworks/UIKit.framework/UIKit:0-[UIInputWindowController postEndNotifications:withInfo:]
Frameworks/UIKit.framework/UIKit:0__77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke_2896
Frameworks/UIKit.framework/UIKit:0-[UIInputWindowController performWithSafeTransitionFrames:]
Frameworks/UIKit.framework/UIKit:0__77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke887
Frameworks/UIKit.framework/UIKit:0-[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:]
Frameworks/UIKit.framework/UIKit:0-[UIViewAnimationState sendDelegateAnimationDidStop:finished:]
Frameworks/UIKit.framework/UIKit:0-[UIViewAnimationState animationDidStop:finished:]
Frameworks/QuartzCore.framework/QuartzCore:0<redacted>
/usr/lib/system/libdispatch.dylib:0_dispatch_client_callout
/usr/lib/system/libdispatch.dylib:0_dispatch_main_queue_callback_4CF
Frameworks/CoreFoundation.framework/CoreFoundation:0__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
Frameworks/CoreFoundation.framework/CoreFoundation:0__CFRunLoopRun
Frameworks/CoreFoundation.framework/CoreFoundation:0CFRunLoopRunSpecific
PrivateFrameworks/GraphicsServices.framework/GraphicsServices:0GSEventRunModal
Frameworks/UIKit.framework/UIKit:0-[UIApplication runModal:]
Frameworks/UIKit.framework/UIKit:0-[UIAlertView _showAnimated:]
Frameworks/UIKit.framework/UIKit:0-[UIWebView webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:]
Frameworks/CoreFoundation.framework/CoreFoundation:0__invoking___
Frameworks/CoreFoundation.framework/CoreFoundation:0-[NSInvocation invoke]
Frameworks/CoreFoundation.framework/CoreFoundation:0-[NSInvocation invokeWithTarget:]
PrivateFrameworks/WebKitLegacy.framework/WebKitLegacy:0<redacted>
Frameworks/CoreFoundation.framework/CoreFoundation:0___forwarding___
Frameworks/CoreFoundation.framework/CoreFoundation:0__forwarding_prep_0___
PrivateFrameworks/WebKitLegacy.framework/WebKitLegacy:0<redacted>
PrivateFrameworks/WebCore.framework/WebCore:0<redacted>
PrivateFrameworks/WebCore.framework/WebCore:0<redacted>
Frameworks/JavaScriptCore.framework/JavaScriptCore:0<redacted>
Frameworks/JavaScriptCore.framework/JavaScriptCore:0<redacted>
Frameworks/JavaScriptCore.framework/JavaScriptCore:0<redacted>
Frameworks/JavaScriptCore.framework/JavaScriptCore:0<redacted>
Frameworks/JavaScriptCore.framework/JavaScriptCore:0<redacted>
Frameworks/JavaScriptCore.framework/JavaScriptCore:0<redacted>
Frameworks/JavaScriptCore.framework/JavaScriptCore:0<redacted>
Frameworks/JavaScriptCore.framework/JavaScriptCore:0JSC::call(JSC::ExecState*, JSC::JSValue, JSC::CallType, JSC::CallData const&, JSC::JSValue, JSC::ArgList const&, WTF::NakedPtr<JSC::Exception>&)
PrivateFrameworks/WebCore.framework/WebCore:0<redacted>
PrivateFrameworks/WebCore.framework/WebCore:0<redacted>
PrivateFrameworks/WebCore.framework/WebCore:0<redacted>
PrivateFrameworks/WebCore.framework/WebCore:0<redacted>
PrivateFrameworks/WebCore.framework/WebCore:0<redacted>
PrivateFrameworks/WebCore.framework/WebCore:0<redacted>
PrivateFrameworks/WebCore.framework/WebCore:0WebCore::EventHandler::keyEvent(WebCore::PlatformKeyboardEvent const&)
PrivateFrameworks/WebCore.framework/WebCore:0WebCore::EventHandler::keyEvent(WebEvent*)
Frameworks/CoreFoundation.framework/CoreFoundation:0__invoking___
Frameworks/CoreFoundation.framework/CoreFoundation:0-[NSInvocation invoke]
Frameworks/CoreFoundation.framework/CoreFoundation:0-[NSInvocation invokeWithTarget:]
Frameworks/UIKit.framework/UIKit:0-[UIThreadSafeNode forwardInvocation:]
Frameworks/CoreFoundation.framework/CoreFoundation:0___forwarding___
Frameworks/CoreFoundation.framework/CoreFoundation:0__forwarding_prep_0___
Frameworks/UIKit.framework/UIKit:0-[UIKeyboardImpl _handleWebKeyEvent:withEventType:withInputString:withInputStringIgnoringModifiers:]
Frameworks/UIKit.framework/UIKit:0__78-[UIKeyboardImpl _handleWebKeyEvent:withIndex:inInputString:executionContext:]_block_invoke
Frameworks/UIKit.framework/UIKit:0-[UIKeyboardTaskExecutionContext returnExecutionToParentWithInfo:]
Frameworks/UIKit.framework/UIKit:0-[UIKeyboardTaskExecutionContext returnExecutionToParentWithInfo:]
Frameworks/UIKit.framework/UIKit:0-[UIKeyboardTaskQueue continueExecutionOnMainThread]
Frameworks/Foundation.framework/Foundation:0__NSThreadPerformPerform
Frameworks/CoreFoundation.framework/CoreFoundation:0__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
Frameworks/CoreFoundation.framework/CoreFoundation:0__CFRunLoopDoSources0
Frameworks/CoreFoundation.framework/CoreFoundation:0__CFRunLoopRun
Frameworks/CoreFoundation.framework/CoreFoundation:0CFRunLoopRunSpecific
PrivateFrameworks/GraphicsServices.framework/GraphicsServices:0GSEventRunModal
Frameworks/UIKit.framework/UIKit:0UIApplicationMain
MyApp:0mh_execute_header
/usr/lib/system/libdyld.dylib:0<redacted>


I really am not sure as to why is this crash happening.

TIA!

Answer

I was able to get this crash fixed by doing the following:

  1. Put a Javascript callback when the keyboard is dismissed
  2. Fetch the callback in

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool

  1. Set the content offset of WebView's ScrollView:

webView.scrollView.setContentOffset(CGPointMake(0, 0), animated: false)

I had a bunch of WebViews inside a ScrollView. I know this is not ideal, but I had to go ahead with this.

When the keyboard is dismissed, the WebView tries to scroll to a position which does not exist and cause the app to crash.

So by doing the callback method, I scroll the WebView to the top, once the keyboard is dismissed.

Comments