ios - UIWebview [UIThreadSafeNode _responderForEditing] 崩溃
<p><p>所以我有一个基于 webview 的应用程序。我做了一些键盘修改(不是问题,将它们全部删除以进行故障排除)。</p>
<p>在 iPhone 6+ 模拟器上,当处于横向模式时,如果复制了某些内容,并且您使用键盘粘贴按钮(不是上下文按钮),应用程序会崩溃。我在尝试使用键盘剪切按钮时也注意到了这一点。</p>
<p>以下崩溃如下:</p>
<pre><code> -: unrecognized selector sent to instance 0x7ff4364344c0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-: unrecognized selector sent to instance 0x7ff4364344c0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010df98f45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010da12deb objc_exception_throw + 48
2 CoreFoundation 0x000000010dfa156d - + 205
3 CoreFoundation 0x000000010deeeeea ___forwarding___ + 970
4 CoreFoundation 0x000000010deeea98 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010c31eb4a - + 1445
6 UIKit 0x000000010c878ef0 - + 278
7 UIKit 0x000000010c3169de - + 1525
8 UIKit 0x000000010c31de57 - + 533
9 UIKit 0x000000011df96fb5 - + 61
10UIKit 0x000000010c8793ab - + 332
11UIKit 0x000000010c1347bd - + 159
12UIKit 0x000000010c1352ef - + 415
13UIKit 0x000000010bee2cc2 - + 308
14UIKit 0x000000010bee3c06 - + 865
15UIKit 0x000000010be932fa - + 263
16UIKit 0x000000011df66a29 - + 77
17UIKit 0x000000010be6dabf _UIApplicationHandleEventQueue + 6844
18CoreFoundation 0x000000010dec5011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
19CoreFoundation 0x000000010debaf3c __CFRunLoopDoSources0 + 556
20CoreFoundation 0x000000010deba3f3 __CFRunLoopRun + 867
21CoreFoundation 0x000000010deb9e08 CFRunLoopRunSpecific + 488
22GraphicsServices 0x000000010ead2ad2 GSEventRunModal + 161
23UIKit 0x000000010be7330d UIApplicationMain + 171
</code></pre>
<p>显然,因为我使用的是 webview,所以没有实际的 textview 可以绑定(bind)到它。</p>
<p>我将如何解决这个问题?我不关心通过应用商店审查,所以欢迎私有(private) API 或其他非苹果批准的方法。</p>
<p>任何帮助将不胜感激。谢谢。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这是Apple的错误,因为这个选择器应该传递给<code>UIResponder</code>,但<code>UIThreadSafeNode</code>是<code>NSObject</code>的子类。为了解决这个问题,您可以实现 <code>NSObject</code> 类别:</p>
<pre><code>@interface UIView (FirstResponder)
-(id)findFirstResponder;
@end
@implementation UIView (FirstResponder)
- (id)findFirstResponder
{
if (self.isFirstResponder) {
return self;
}
for (UIView *subView in self.subviews) {
id responder = ;
if (responder) return responder;
}
return nil;
}
@end
@interface NSObject (KeyboardBUI)
-(_Nonnull id)_responderForEditing;
@end
@implementation NSObject (KeyboardBUI)
-(_Nonnull id)_responderForEditing
{
return self;
}
-(void)cut:(nullable id)sender
{
NSString *selection = [ stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
[ stringByEvaluatingJavaScriptFromString:@"document.execCommand('delete', false, null)"];
.string = selection;
}
-(void)paste:(nullable id)sender
{
NSString *text = .string;
[ stringByEvaluatingJavaScriptFromString:];
}
-(void)copy:(nullable id)sender
{
NSString *selection = [ stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
.string = selection;
}
-(void)toggleItalics:(nullable id)sender
{
[ stringByEvaluatingJavaScriptFromString:@"document.execCommand(\"Italic\")"];
}
-(void)toggleUnderline:(nullable id)sender
{
[ stringByEvaluatingJavaScriptFromString:@"document.execCommand(\"Underline\")"];
}
-(void)toggleBoldface:(nullable id)sender
{
[ stringByEvaluatingJavaScriptFromString:@"document.execCommand(\"Bold\")"];
}
-(UIWebView * __nullable)getWebView
{
UIWebView *retVal = nil;
id obj = [[[[ keyWindow] findFirstResponder] superview] superview];
if (]) {
retVal = obj;
}
return retVal;
}
@end
</code></pre>
<p>注意:我不知道这是否会通过 Apple Appstore 审核。</p>
<p><code>UIView (FirstResponder)</code> 类别来自 <a href="https://stackoverflow.com/questions/1823317/get-the-current-first-responder-without-using-a-private-api" rel="noreferrer noopener nofollow">answer</a> .</p>
<p>从此 <a href="https://stackoverflow.com/a/16622300/3488699" rel="noreferrer noopener nofollow">answer</a> 复制/粘贴/剪切</p></p>
<p style="font-size: 20px;">关于ios - UIWebview 崩溃,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33807812/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33807812/
</a>
</p>
页:
[1]