Make an touchesbegan event to uiwebview

514 Views Asked by At

I hope you can help me and first sorry for my bad english. I want to tap an specify point (x/y) on an uiwebview in iOS. I have tried something with

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { }

and

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [webview touchesBegan:touches withEvent:event]; }

But i does not work yet. The uiwebview is a subview of uiview, i think.

Can you help me?

Greetings from Germany.

2

There are 2 best solutions below

9
On

Just call a javascript function like this:

[myWebView stringByEvaluatingJavaScriptFromString:"/*JS Code goes here*/"];

instead of /*JS Code goes here*/ type a java script code to do what you want to do by firing the click on the x,y position you want. I am assuming you can control the web page content if not please comment and I will add more complex solution.

If you want to call a function that is defined inside an iframe that is included inside your page content you need to define two functions:

1- JS Function inside the iframe page that appears as source for the iframe tag name this function iFrameFunc()

2- JS Function in the parent HTML page name it parentFunc()

3- parentFunc should get handle to the iframe by id or class selector and call the iframeFunc()

4- Obj-c would call the [UIWebview stringByEvaluatingJavaScriptFromString] to the parentFunc

0
On

I subclassed UIWebView and just "leeched" onto its gesture recognizers in subviews 2 levels deep (you could go recursively but thats enough for iOS6-7). Then you can do whatever you want with the touch location and gesture recognizer's state.

for (UIView* view in self.subviews) {
    for (UIGestureRecognizer* recognizer in view.gestureRecognizers) {
        [recognizer addTarget:self action:@selector(touchEvent:)];
    }
    for (UIView* sview in view.subviews) {
        for (UIGestureRecognizer* recognizer in sview.gestureRecognizers) {
            [recognizer addTarget:self action:@selector(touchEvent:)];
        }
    }
}