2015-11-19 60 views
2

所以我有一個應用程序,是基於網頁視圖。我做了一些鍵盤修改(不是這個問題,將它們全部刪除以進行故障排除)。的UIWebView [UIThreadSafeNode _responderForEditing]崩潰

在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 

很顯然,因爲我使用的網頁視圖,沒有實際的TextView紮了進去。

我將如何解決這個問題?我不在乎通過應用商店評論,因此歡迎使用私有API或其他非Apple批准的方法。

任何援助將不勝感激。謝謝。

回答

2

這是蘋果的bug,因爲這個選擇器應該傳遞給UIResponder,但UIThreadSafeNodeNSObject的一個子類。爲了workarount這一點,您可以實現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)cut:(nullable id)sender 
{ 
    NSString *selection = [[self getWebView] stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"]; 
    [[self getWebView] stringByEvaluatingJavaScriptFromString:@"document.execCommand('delete', false, null)"]; 
    [UIPasteboard generalPasteboard].string = selection; 
} 

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

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

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

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

-(void)toggleBoldface:(nullable 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 

注:我不知道這是否會通過蘋果AppStore審查。從這個answer

UIView (FirstResponder)類別。

複製/粘貼/從這個answer

+0

我應該已經更新了這個問題切,因爲現在我已經使用WKWebView開關。所以這不再是一個問題。但我很欣賞答案。我會鼓勵,並希望如果這能幫助有相同情況的人,他們可以標記爲已接受的答案。 – ChrisOSX

+0

這個工作原理除了在'paste'中構建'execCommand'字符串時,您應該「複製」複製文本中的任何撇號。一種方法是:'NSString * text = [[UIPasteboard generalPasteboard] .string stringByReplacingOccurrencesOfString:@「'」withString:@「'」];'' – Craig