I have created a sample app with the function from the other SO question mentioned by hooleyhoop in the comments.
This is what I end up with:
- (void)webViewDidFinishLoad:(UIWebView *)wv {
// Called when the website has finished loading and triggers the JS code
NSString *javascript = @"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) { \
alert('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 \
}; \
window.setTimeout(function() { \
var links = document.getElementsByTagName('a'); \
for(var i in links) { \
if(links[i].innerHTML == 'Test') { \
simulate(links[i], 'click'); \
} \
} \
},1);";
// Execute JS
[webView stringByEvaluatingJavaScriptFromString:javascript];
}
Test HTML
<html>
<head>
<title>Testpage</title>
</head>
<body>
<a href="http://www.google.com">I'm linking to google.com</a><br />
<a href="http://www.stackoverflow.com">Test</a>
</body>
</html>
- (void)webViewDidFinishLoad:(UIWebView *)wv
This is the UIWebView's delegate function which get's called as soon as the web view has finished loading the website.
NSString *javascript = @"..."
That's the point where I construct the JS code, at the beginning, the function from the other SO question that triggers the click. At the end I placed a timeout that executes the part that looks for all "a" tags, loops through them, checks if it's innerHTML text is "Test" and if that's the case, it calls the simulate
function with the a element as the first parameter and the eventtype as the second parameter. Boom, link clicked.
I used the timeout as it caused problems executing the JS directly, seems like the UIWebView needs some time until the function simulate becomes available.
Cheers,
Björn