see that post
http://blog.techno-barje.fr/post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript/
and that How to call Objective-C from Javascript?
you can send some string to your UIWebView using script
function sendURLToUIWebView(url) {
var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", url);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
}
your url should has specific schem f.e. myappcomand://
and you can handle it with UIWebViewDelegate
method (set some object as delegate of UIWebView, f.e. some UIViewController)
- (BOOL)webView:(UIWebView *)aWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
BOOL shouldLoad = YES;
if ([request.URL.scheme isEqualToString:@"myappcomand"]) {
shouldLoad = NO;
// parse url string "request.URL" and extract your parameters to store them
}
return shouldLoad;
}
your javascript functions
function sendURLToUIWebView(url) {
var iframe = document.createElement("IFRAME");
iframe.setAttribute("src", url);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
}
function getRangeForSelectedText() {
var selection = window.getSelection();
var range = selection.getRangeAt(0);
var url = "myappcomand://" + "range=" + range; // you should convert range to string
sendURLToUIWebView(url);
}
Update:
range to string
range.toString().replace(/\s+/g, ' ').replace(/^\s+|\s+$/g, '')
or just var rangeText = window.getSelection().toString();
see Strange behaviour with range.toString()