Reputation: 302
I have a UIWebview in which html content is being displayed. when a user selects a piece of text, I want to save the range of that text using objective c, then recall that saved range at a later stage to reselect the text in the UIWebview.
The closest I could get is the following javascript:
var selection = window.getSelection();
var range = selection.getRangeAt(selection.rangeCount);
But I have no clue as to how to return the range object back to objective c to save it and reuse it later on. Could any one point me into the right direct?
Thanks, EZFrag
Upvotes: 3
Views: 744
Reputation: 324567
You could serialize the range to a string representing the paths through the DOM to get to the start and end points. My Rangy library has a serializer module that does this, but for just Mobile Safari, relying on a relatively large library just for this task may be overkill. For a more lightweight script that uses the same idea, see Martin Honnen's one here: http://home.arcor.de/martin.honnen/javascript/storingSelection1.html
Upvotes: 2