How to copy selected data from a PDF displayed in

2019-06-12 18:14发布

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条回答
Viruses.
2楼-- · 2019-06-12 18:28

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.

查看更多
登录 后发表回答