PDA

View Full Version : webView - onclick in HTML




estupefactika
Mar 17, 2009, 09:10 AM
Hi, I have a UIWebView, Ive inserted a html with a onclick to handle the action in my function.

NSString *HTMLData = @"
<div onclick='window.location=\"call?my_function\"'>Test</div>
<h1>Hello this is a test</h1>
<img src="sample.jpg" alt="" width="100" height="100" />";

[webView loadHTMLString:HTMLData baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];


When I click in div tag, I handle the action here:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
.....
[self test];
return YES;
}
-(void) test {
NSLog(@"Test");
}


The problem is nothing happends when I click. If i click for a little seconds in div tag it appears a little window entitled "Action".
But if I put:
[webView loadHTMLString:HTMLData baseURL:nil];
It works fine, I receive the call and I handle action in my method, its correct, but I need to load local files in html so I put;
[webView loadHTMLString:HTMLData baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];

When I do this dont works onclick and it appears me the window "Action".

Any idea? Thanks for you help



estupefactika
Mar 17, 2009, 09:18 AM
Fixed :)

jnic
Mar 17, 2009, 09:48 AM
Care to share your solution for anyone who comes across this in future?

estupefactika
Mar 17, 2009, 10:40 AM
Really it was fine, Ive to modified my function in structure 'if' to handle action:


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url=[request URL];

NSString *scheme = [url scheme];
NSString *tempUrl =[url query];
if ([scheme compare:@"applewebdata"] == 0) {
if ([tempUrl isEqualToString:@"my_function"])
[self test];
return YES;
} else if ([scheme compare:@"file"]==0){
if ([tempUrl isEqualToString:@"my_function"])
[self test];

return YES;
} else {
return YES;
}
}