How to simulate tap event on UIWebView? I just need to process mouse down+up events by page scripts.
Asked
Active
Viewed 3,921 times
1 Answers
3
I am not sure whether this is possible with public APIs. Using private APIs, this is possible as mentioned here synthesizing-touch-event-on-iphone. You will not be able to use this in appstore app though.
Another possible way to do this is by using a javascript. Not sure this is exactly what you wanted. It can be done using javascript as mentioned here,
function simulate(element, eventName)
{
var options = extend(defaultOptions, arguments[2] || {});
var oEvent, eventType = null;
for (var name in eventMatchers)
{
if (eventMatchers[name].test(eventName)) { eventType = name; break; }
}
if (!eventType)
throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
if (document.createEvent)
{
oEvent = document.createEvent(eventType);
if (eventType == 'HTMLEvents')
{
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
}
else
{
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
}
element.dispatchEvent(oEvent);
}
else
{
options.clientX = options.pointerX;
options.clientY = options.pointerY;
var evt = document.createEventObject();
oEvent = extend(evt, options);
element.fireEvent('on' + eventName, oEvent);
}
return element;
}
function extend(destination, source) {
for (var property in source)
destination[property] = source[property];
return destination;
}
var eventMatchers = {
'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
}
var defaultOptions = {
pointerX: 0,
pointerY: 0,
button: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
bubbles: true,
cancelable: true
}
and then use,
simulate(document.getElementById("btn"), "click");
You can try to execute javascript by implementing the UIWebView's
delegate method webViewDidFinishLoad:
in your UIViewController
and in there you call [webview stringByEvaluatingJavaScriptFromString:@"methodName()"];
. Check if that helps.
-
Thanks a lot! But how to make event on javascript using the last known mouse position? Is it possible? – Dmitry Nov 11 '12 at 23:44
-
I dont have much experience with javascripts. Check this, if it helps. http://stackoverflow.com/questions/1222958/javascript-create-event-with-current-mouse-coordinates. If that doesn't work you might have to post a new question as it is a different question. – iDev Nov 12 '12 at 00:10
-
It doesn't help me - script doesn't work on my case:( The first link has 18 errors on SDK6. – Dmitry Nov 12 '12 at 10:14
-
I'm sorry, I should call it with mouse coordinates as options to get it working. How to do it? – Dmitry Nov 12 '12 at 10:23
-
1As I said, I am not sure about javascripts, did you check http://stackoverflow.com/questions/1222958/javascript-create-event-with-current-mouse-coordinates – iDev Nov 12 '12 at 10:29