DenVog
DenVog

Reputation: 4286

Pasting Image into UITextView Causes Crash

Can anyone tell me how to avoid a crash caused by pasting an image into a UITextView?

I have UITextView in my app that allows some basic editing of attributed text (iOS 6). A user has discovered that they can copy a combination of text and images from a web page, then switch to my app and use the UITextView menu > paste command it will crash the app. I can reproduce it, but I'm stumped what to do about it. The log from the crash looks like:

*** Assertion failure in -[NSHTMLReader _addAttachmentForElement:URL:needsParagraph:usePlaceholder:], /SourceCache/UIFoundation_Sim/UIFoundation-78/UIFoundation/TextSystem/NSHTMLReader.m:1478
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to find missing_image.tiff!'

I can't stop users from selecting images when they use the copy command in another app. I also don't want to disallow pasting within my app.

Upvotes: 3

Views: 1795

Answers (1)

DenVog
DenVog

Reputation: 4286

It is a terrible hack, but I did find a way to avoid the crash. In short, I've subclassed UITextView to override the paste command and converted the pasteboard to a string that is insert into the UITextView.

- (void)paste:(id)sender {    
    UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
    NSString *string = pasteBoard.string;
    NSLog(@"Pasteboard string: %@", string);
//  [pasteBoard setValue:@"Please work you miserable bastard!" forPasteboardType:(NSString *)kUTTypeUTF8PlainText];
//  [super paste:sender]; // Doesn't work for unknown reason.
    [self insertText:string];
}

Welome any better suggestions...

Upvotes: 5

Related Questions