webView - onclick in HTML

Discussion in 'iOS Programming' started by estupefactika, Mar 17, 2009.

  1. estupefactika macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
    #1
    Hi, I have a UIWebView, Ive inserted a html with a onclick to handle the action in my function.
    Code:
    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:
    Code:
    - (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
     
  2. estupefactika thread starter macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
  3. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #3
    Care to share your solution for anyone who comes across this in future?
     
  4. estupefactika thread starter macrumors member

    Joined:
    Feb 16, 2009
    Location:
    Alcobendas (Madrid)
    #4
    Really it was fine, Ive to modified my function in structure 'if' to handle action:

    Code:
    - (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;
    	}
    }
    
    
     

Share This Page