I have a UIWebView which loads a HTML string and content can be added to doing using Javascript.
Is there any way to know when something (text or an image) has been pasted in the web view.
NSString *path = [[NSBundle mainBundle] pathForResource:@"htmlString" ofType:@"txt"];
NSString *htmlString = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.ibWebView loadHTMLString:htmlString baseURL:nil];
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *jsToEncloseInEditableContainer = [NSString stringWithFormat:@"function contentedited(e){window.location='mpscontentedited://' + e.keyCode;};(function() {var body = document.body;var contentContainer = document.createElement('div');contentContainer.id = 'content';contentContainer.setAttribute('contenteditable','true');contentContainer.style.fontFamily=' Helvetica';contentContainer.style.marginTop=\"15px\";contentContainer.onkeydown = contentedited;contentContainer.onpaste=contentedited;var node;while(node=body.firstChild) {contentContainer.appendChild(node);}body.appendChild(contentContainer);%@body=undefined;delete body;contentContainer=undefined;delete contentContainer;node=undefined;delete node;})();", @"contentContainer.focus();"];
[self.ibWebView stringByEvaluatingJavaScriptFromString:jsToEncloseInEditableContainer];
}
I have tried overriding UIResponder's paste: method, but it never gets called.
- (void)paste:(id)sender
{
NSLog(@"paste not called");
}
I have also tried to creating a custom menu button
UIMenuController *menu = [UIMenuController sharedMenuController];
UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"Paste Custom" action:@selector(pasteCustom:)];
[menu setMenuItems:@[item]];
[menu setMenuVisible:YES];
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(pasteCustom:))
{
return YES;
}
return [super canPerformAction:action withSender:sender];
}
- (void)pasteCustom:(id)sender
{
NSLog(@"paste custom");
[super paste:sender]; // --> calling this causes a crash (unrecognized selector sent to instance)
}
What am I doing wrong?
I either want to know when something is pasted or I want to have a custom menu button that behaves similar to default paste button.
Thanks in advance.