• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - UIWebview [UIThreadSafeNode _responderForEditing] 崩溃

[复制链接]
菜鸟教程小白 发表于 2022-12-12 17:16:20 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

所以我有一个基于 webview 的应用程序。我做了一些键盘修改(不是问题,将它们全部删除以进行故障排除)。

在 iPhone 6+ 模拟器上,当处于横向模式时,如果复制了某些内容,并且您使用键盘粘贴按钮(不是上下文按钮),应用程序会崩溃。我在尝试使用键盘剪切按钮时也注意到了这一点。

以下崩溃如下:

    -[UIThreadSafeNode _responderForEditing]: unrecognized selector sent to instance 0x7ff4364344c0
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIThreadSafeNode _responderForEditing]: 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 -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010deeeeea ___forwarding___ + 970
    4   CoreFoundation                      0x000000010deeea98 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010c31eb4a -[UIKeyboardLayoutStar touchDownWithKey:atPoint:executionContext:] + 1445
    6   UIKit                               0x000000010c878ef0 -[UIKeyboardTaskExecutionContext returnExecutionToParentWithInfo:] + 278
    7   UIKit                               0x000000010c3169de -[UIKeyboardLayoutStar performHitTestForTouchInfo:touchStage:executionContextPassingUIKBTree:] + 1525
    8   UIKit                               0x000000010c31de57 -[UIKeyboardLayoutStar touchDown:executionContext:] + 533
    9   UIKit                               0x000000011df96fb5 -[UIKeyboardLayoutStarAccessibility touchDown:executionContext:] + 61
    10  UIKit                               0x000000010c8793ab -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 332
    11  UIKit                               0x000000010c1347bd -[UIKeyboardLayout touchDown:] + 159
    12  UIKit                               0x000000010c1352ef -[UIKeyboardLayout touchesBegan:withEvent:] + 415
    13  UIKit                               0x000000010bee2cc2 -[UIWindow _sendTouchesForEvent:] + 308
    14  UIKit                               0x000000010bee3c06 -[UIWindow sendEvent:] + 865
    15  UIKit                               0x000000010be932fa -[UIApplication sendEvent:] + 263
    16  UIKit                               0x000000011df66a29 -[UIApplicationAccessibility sendEvent:] + 77
    17  UIKit                               0x000000010be6dabf _UIApplicationHandleEventQueue + 6844
    18  CoreFoundation                      0x000000010dec5011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    19  CoreFoundation                      0x000000010debaf3c __CFRunLoopDoSources0 + 556
    20  CoreFoundation                      0x000000010deba3f3 __CFRunLoopRun + 867
    21  CoreFoundation                      0x000000010deb9e08 CFRunLoopRunSpecific + 488
    22  GraphicsServices                    0x000000010ead2ad2 GSEventRunModal + 161
    23  UIKit                               0x000000010be7330d UIApplicationMain + 171

显然,因为我使用的是 webview,所以没有实际的 textview 可以绑定(bind)到它。

我将如何解决这个问题?我不关心通过应用商店审查,所以欢迎私有(private) API 或其他非苹果批准的方法。

任何帮助将不胜感激。谢谢。



Best Answer-推荐答案


这是Apple的错误,因为这个选择器应该传递给UIResponder,但UIThreadSafeNodeNSObject的子类。为了解决这个问题,您可以实现 NSObject 类别:

@interface UIView (FirstResponder)

-(id)findFirstResponder;

@end

@implementation UIView (FirstResponder)

- (id)findFirstResponder
{
    if (self.isFirstResponder) {
        return self;
    }
    for (UIView *subView in self.subviews) {
        id responder = [subView findFirstResponder];
        if (responder) return responder;
    }
    return nil;
}

@end


@interface NSObject (KeyboardBUI)

-(_Nonnull id)_responderForEditing;

@end

@implementation NSObject (KeyboardBUI)

-(_Nonnull id)_responderForEditing
{
    return self;
}

-(void)cutnullable id)sender
{
    NSString *selection = [[self getWebView] stringByEvaluatingJavaScriptFromString"window.getSelection().toString()"];
    [[self getWebView] stringByEvaluatingJavaScriptFromString"document.execCommand('delete', false, null)"];
    [UIPasteboard generalPasteboard].string = selection;
}

-(void)pastenullable id)sender
{
    NSString *text = [UIPasteboard generalPasteboard].string;
    [[self getWebView] stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat"document.execCommand('insertHTML', false, '%@')", text]];
}

-(void)copynullable id)sender
{
    NSString *selection = [[self getWebView] stringByEvaluatingJavaScriptFromString"window.getSelection().toString()"];
    [UIPasteboard generalPasteboard].string = selection;
}

-(void)toggleItalicsnullable id)sender
{
    [[self getWebView] stringByEvaluatingJavaScriptFromString"document.execCommand(\"Italic\")"];
}

-(void)toggleUnderlinenullable id)sender
{
    [[self getWebView] stringByEvaluatingJavaScriptFromString"document.execCommand(\"Underline\")"];
}

-(void)toggleBoldfacenullable id)sender
{
    [[self getWebView] stringByEvaluatingJavaScriptFromString"document.execCommand(\"Bold\")"];
}

-(UIWebView * __nullable)getWebView
{
    UIWebView *retVal = nil;
    id obj = [[[[[UIApplication sharedApplication] keyWindow] findFirstResponder] superview] superview];
    if ([obj  isKindOfClass:[UIWebView class]]) {
        retVal = obj;
    }
    return retVal;
}

@end

注意:我不知道这是否会通过 Apple Appstore 审核。

UIView (FirstResponder) 类别来自 answer .

从此 answer 复制/粘贴/剪切

关于ios - UIWebview [UIThreadSafeNode _responderForEditing] 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807812/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap