Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notifications in UIWebView that text selection did start or end?

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.

like image 266
Stephan Heilner Avatar asked Oct 30 '12 19:10

Stephan Heilner


1 Answers

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.

like image 152
mattsven Avatar answered Nov 08 '22 20:11

mattsven