0

I'm trying to save the text selected in my app webView into a String. how is it even possible? As far as I've checked apple don't let you add options to their (copy, cut) pop up menu. I've seen some menu pop ups in github but they are only menus and you cannot select any text from the webView. Plus, I don't want to grab the whole text(or body), just the selected text that the user choose. any suggestion?

EDIT: I do control the webView, my app is RSS kind of app and theres an inner webView which you can see the article. I'm using stringByEvaluatingJava.. to get the document title like this

NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];  

however, I don't know how to grab the content when the user select the text.

XcodeNOOB
  • 2,135
  • 4
  • 23
  • 29
  • Do you control the code in the webview? You can always bridge between obj-c and javascript. – Kevin DiTraglia Jan 05 '15 at 22:27
  • Hey kevin, I've edit my question. – XcodeNOOB Jan 05 '15 at 22:40
  • Here's a question about calling obj-c code from javascript: http://stackoverflow.com/a/9827105/1316346 – Kevin DiTraglia Jan 05 '15 at 22:42
  • Essentially you have the javascript reload the url with a something your app can parse in the `shouldStartLoadRequest` delegate method of the webView, then ignore the request if it matches a command your app is to do. It's a little hacky, but gets the job done. – Kevin DiTraglia Jan 05 '15 at 22:43
  • I don't get it, how the information you've send me can save me the user text selection into a string? – XcodeNOOB Jan 05 '15 at 22:47
  • I can write up a detailed answer with code when I get home. But essentially you want to write javascript that handles the event of the user selecting text. Once you have the text in the javascript, use the technique in the above answer I linked to pass the text to your obj-c app. – Kevin DiTraglia Jan 05 '15 at 22:49
  • Thank you so much, waiting for the detailed answer Since I'm not familiar with java that much. – XcodeNOOB Jan 05 '15 at 23:05
  • Let me just clarify beforehand. You have access to the code (HTML / javascript) of the webpage you are displaying in the webview? Or you are pulling a website from some other web server on the web that you do not control? – Kevin DiTraglia Jan 05 '15 at 23:18
  • I do have access since I'm loading the website in my own webView inside my app. – XcodeNOOB Jan 06 '15 at 15:52
  • I meant more along the lines of, do you have access to the web server that's hosting the content. You can always inject javascript to somebody else's page, but it gets a lot hairier. It's a lot easier to build javascript hooks into the hosted page itself. – Kevin DiTraglia Jan 06 '15 at 16:07
  • Nop, I don't not have access to the web server . – XcodeNOOB Jan 07 '15 at 01:05

1 Answers1

0

Give this a go:

NSString *userSelection = [self.webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];
applejack42
  • 1,155
  • 9
  • 18
  • yup tried it already. when I select a text I get copy,define. of course when I NSLogging it I get nothing. I've put the String in the webViewDidFinishLoad method. – XcodeNOOB Jan 06 '15 at 15:50