• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How to get selected text in a WebView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to get selected text in a WebView


  • Subject: How to get selected text in a WebView
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Mon, 4 Oct 2010 14:15:09 +0700

I have a Webview, and I want to do something with the selected text.

I tried:
DOMRange *dr = [ webView selectedDOMRange ];
NSString *m = [ dr markupString ];
this contains the selected characters, but buried in lots of markup language. But I just want the characters.

NSString *s= [ dr stringRepresentation ];
just returns "Undefined".

I could do [webView copy:nil ]; and then get the string from the general pasteboard, but I rather do not like to mess up the pasteboard - the user might have put something important there.

So I tried:
NSPasteboard *pasteboard = [ NSPasteboard pasteboardWithUniqueName ];
NSArray *types = [ NSArray arrayWithObject: NSPasteboardTypeString ];
[ webView writeSelectionWithPasteboardTypes: types  toPasteboard: pasteboard ];
NSString *po = [ pasteboard stringForType: NSPasteboardTypeString ];
NSLog(@"%s selectedString \"%@\"",__FUNCTION__,po);

but the result is:  selectedString "(null)"
Adding [ pasteboard declareTypes: types owner: nil ] does not make any changes.

So what am I doing wrong?
How to get the selection as string form a WebView?


Kind regards,

Gerriet.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: How to get selected text in a WebView
      • From: "Gary L. Wade" <email@hidden>
    • Re: How to get selected text in a WebView
      • From: Keary Suska <email@hidden>
  • Prev by Date: Re: Why isn't this delegate protocol being seen?
  • Next by Date: Instantly delete a directory without recursion?
  • Previous by thread: Re: Why isn't this delegate protocol being seen?
  • Next by thread: Re: How to get selected text in a WebView
  • Index(es):
    • Date
    • Thread