How to copy selected data from a PDF displayed in a UIWebView

好久不见. 提交于 2020-01-14 06:21:04

问题


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

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