5

Currently I am taking this sample website and showing in my webview. The webpage is displaying correctly.

Now I am trying to figure out which data is selected once the user has tapped on the uiwebview.

For this I am able to get the CGPoint for the tap by using UITapGestureRecognizer.

-(void)singleTap:(UIGestureRecognizer *)gestureRecognizer
{

    CGPoint touchPoint = [gestureRecognizer locationInView:myWebView];

    NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).toString()", touchPoint.x, touchPoint.y];

    NSString * tagName = [myWebView stringByEvaluatingJavaScriptFromString:js];

    NSLog(@"Selected Name: %@",tagName); 

}

// In log it is displaying [object SVGPathElement]

I want to get the exact data once user selects the vertical bar in first graph (E.g. 1994/1995/1996).

How to do this?

2
  • Is the HTML page under your control?
    – sergio
    Apr 7, 2012 at 19:36
  • No its not under my control.But still you can suggest your opnion :)
    – raaz
    Apr 8, 2012 at 6:04

3 Answers 3

2

You don't specify what is that is not working for you... anyway, a couple of suggestions:

  1. try with the following js in your tap handler:

    NSString *js = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).innerHTML", touchPoint.x, touchPoint.y];
    
  2. when creating your tap gesture handler, specify a delegate for it (it can be your controller):

    tap1.delegate = self;
    
  3. in your controller (or web view delegate), define the following delegate method:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:    (UIGestureRecognizer*)otherGestureRecognizer {
        return YES;
    }
    
  4. if you are using iOS 5, have a look at this article about a glitch in elementFromPoint.

By doing like this, I am able to get the exact HTML value for the selected object.

1
  • Be warned this doesn't work well for Google images or other sites that display images in funny ways...
    – jjxtra
    Jan 10, 2014 at 0:03
0

If they're all independent DOM elements, you should be able to listen for touchstart events in JavaScript, and when you get the event object, it should have event.target, this will be the element the user clicked on. You can then capture the data you need in JS, and then send it back to Objective-c. The best way to get data from JS to native code is to use a UIWebView delegate, this should hopefully do the trick for you!

0

When user make a selection on the UIWebView, make an AJAX http request, that you can intercept in your UIWebViewDelegate implementing the

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

method.

Then you can accept the request loading, block it, analyze it, do whatever you like with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.