This approach has worked for me in the past:
DOMElement *el = // find element somehow
DOMDocument *doc = [el ownerDocument];
DOMAbstractView *window = [doc defaultView];
DOMUIEvent *evt = (DOMUIEvent *)[doc createEvent:@"UIEvents"];
[evt initUIEvent:@"click" canBubble:YES cancelable:YES view:window detail:1];
[el dispatchEvent:evt];
I believe that is all standard DOM2 Events module APIs. I think I found this solution from looking at the JavaScript on this MozDev createEvent
article.
Also note the -[DOMHTMLElement click]
method in WebKit's DOMHTMLElement.h which is marked as AVAILABLE_AFTER_WEBKIT_VERSION_5_1
.
Finally, I have occasionally had to resort to something hackier to fully simulate a user "mouse click" and all the DOM events that usually accompany it. I would only try this if nothing else above works (this is complex, ugly and brittle):
WebView *webView = // get web view
DOMElement *el = // find element somehow
id relatedTarget = [(DOMHTMLDocument *)[el ownerDocument] body];
[el dispatchMouseEventType:@"mouseover" clickCount:0 ctrlKey:NO altKey:NO shiftKey:NO metaKey:NO button:0 relatedTarget:relatedTarget webView:webView];
[el dispatchMouseEventType:@"mousemove" clickCount:0 ctrlKey:NO altKey:NO shiftKey:NO metaKey:NO button:0 relatedTarget:nil webView:webView];
[el dispatchMouseEventType:@"mousedown" clickCount:1 ctrlKey:NO altKey:NO shiftKey:NO metaKey:NO button:0 relatedTarget:nil webView:webView];
[el dispatchMouseEventType:@"click" clickCount:1 ctrlKey:NO altKey:NO shiftKey:NO metaKey:NO button:0 relatedTarget:nil webView:webView];
[el dispatchMouseEventType:@"mouseup" clickCount:1 ctrlKey:NO altKey:NO shiftKey:NO metaKey:NO button:0 relatedTarget:nil webView:webView];
[el dispatchMouseEventType:@"mousemove" clickCount:0 ctrlKey:NO altKey:NO shiftKey:NO metaKey:NO button:0 relatedTarget:nil webView:webView];
[el dispatchMouseEventType:@"mouseout" clickCount:0 ctrlKey:NO altKey:NO shiftKey:NO metaKey:NO button:0 relatedTarget:relatedTarget webView:webView];
I have a lot of this code in my open source project TDAppKit on Google Code.