Search code examples
iosobjective-ciphoneswiftuiwebview

Getting the selected text of a UIWebView


I'm trying to access the currently selected text in a UIWebView using the following line of code:

NSString *highlighted = [_webView stringByEvaluatingJavaScriptFromString:@"window.getSelection();"];

But it only returns an emtpy string. Any ideas about what I'm missing?


Solution

  • Try with this :

    NSString * highlighted = [_webView stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"];