问题
I am working on a project that displays a PDF in a UIWebView. I have added another UIMenuItem to the UIMenuController in order to do various things with the text in the UIWebView that has been selected. The problem is that I am not able to access the selected text in my newly created selector method. If I use the copy command on selected text first, then I can get that previously copied text from the pasteboard, but a command like [myWebView copy:sender]; called from my new selector does nothing. How can I receive the selected text in my new selector? I know this can be done easily with javascript when working with HTML in a UIWebView, how do people usually do this with PDF files displayed in a UIWebView?
回答1:
You can call the copy command from the first responder using this code:
[[UIApplication sharedApplication] sendAction:@selector(copy:) to:nil from:self forEvent:nil];
Then you can simply retrieve it from the pasteboard:
[UIPasteboard generalPasteboard].string;
This seems to be the only way to receive the selected text from a UIWebView that is displaying a PDF, the javascript methods will not work with PDF files, only HTML.
来源:https://stackoverflow.com/questions/12623334/how-to-copy-selected-data-from-a-pdf-displayed-in-a-uiwebview