I'm looking for a way to observe for notifications in a UIWebView that text selection has either started or ended. Once the pins and blue selection overlay appear, it updates the Javascript Selection and Range objects automatically, but when you tap out of the selection and it ends, there doesn't seem to be a clean way to get notified.
You could simply just latch on to the javascript touchend
event (or touchesEnded
in Obj-C) and check the status of the window.getSelection()
. There's also the javascript event selectionchange
.
Another method that I use is to check the status of the UIWebView's selection via looping through subviews:
- (bool) selectionInWebView:(UIWebView*)webView {
UIScrollView *scrollView = webView.scrollView; //assumes IO(6)? and scrollView is exposed. Loop subviews otherwise.
UIView *browserView;
for(int i = 0; scrollView.subviews.count; i++){
if([NSStringFromClass([scrollView.subviews[i] class]) isEqualToString:@"UIWebBrowserView"]){
browserView = scrollView.subviews[i]; break;
}
}
if(browserView == nil) return false;
for(int i = 0; browserView.subviews.count; i++){
if([NSStringFromClass([browserView.subviews[i] class]) isEqualToString:@"UIWebSelectionView"]){
//UIView *selectionView = browserView.subviews[i];
return true; //selection view exists, a selection is in progress
}
}
return false;
}
But your best option is checking the status of selection when the touch end events fire or selection change event fires.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With