iOS UIWebView TextField - 输入文本后点击键盘上的“完成”按钮时应用程序崩溃

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

我的应用程序中有多个网页视图。很少有 webviews 中会有文本字段。

最初 运行 应用程序时,在文本字段中输入一些文本并点击键盘上的 'Done' 按钮会导致应用程序崩溃。如果我重新启动该应用程序并尝试相同的场景,它会正常工作。

我在 WebView 中还有 运行 几个 Javascript 文件。

这是抛出的错误:

EXC_BAD_ACCESS mh_execute_header
Attempted to dereference null pointer.

这是我从崩溃细节中得到的痕迹:

/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>

我真的不确定为什么会发生这种崩溃。

TIA!

我通过执行以下操作修复了此崩溃:

  1. 在键盘关闭时放置一个 Javascript 回调
  2. 中获取回调

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

  1. 设置WebView的ScrollView的内容偏移量:

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

我在 ScrollView 中有一堆 WebView。我知道这并不理想,但我不得不继续这样做。

关闭键盘时,WebView 会尝试滚动到不存在的位置并导致应用程序崩溃。

因此,通过执行回调方法,我将 WebView 滚动到顶部,一旦键盘被关闭。