问题
I can't seem to get the execCommand for cut, copy, paste to work in a UIWebView that has contentEditable set to true. I am able to select the text using selectAll command but cut, copy, and paste do not work.
This is the code I'm using:
[webView stringByEvaluatingJavaScriptFromString:@"document.execCommand('cut', false, null)"];
Is there something else I need to do to allow clipboard operations to work?
回答1:
As per previous comments cut, copy, paste commands don't appear to work so I managed to achieve the same operations like so:
- (void)cut
{
NSString *selection = [self.webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
[webView stringByEvaluatingJavaScriptFromString:@"document.execCommand('delete', false, null)"];
[UIPasteboard generalPasteboard].string = selection;
}
- (void)paste
{
NSString *text = [UIPasteboard generalPasteboard].string;
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.execCommand('insertHTML', false, '%@')", text]];
[UIPasteboard generalPasteboard].string = @"";
}
回答2:
I'm not 100% sure, but I don't believe it is possible to invoke cut, copy, or paste programmatically in a UIWebView. You can, however get the selected text, using window.getSelection(). You can do what you want with the selection from there.
来源:https://stackoverflow.com/questions/16622032/uiwebview-execcommand-cut-copy-paste