UIWebView execCommand Cut, Copy, Paste

試著忘記壹切 提交于 2019-12-05 06:54:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!