user2462483
user2462483

Reputation: 121

passing parameters from objective to javascript

I want to send some NSString value from objective-c to javascript. Here is code :

Javascript.html

<!DOCTYPE html>
<html>
    <head>
        <title>Sample Page that Writes Out an HTML Addition Table</title>
        <script src=“doc.js”></script>
    </head>
    <body>
    </body>
</html>

doc.js

function call(str){
    document.write('<table border="1" cellspacing="1" cellpadding="5">')
    for(i = 0; i < 4; i++){
        document.write('<tr>')
        for (j = 0; j < 3; j++){
            document.write('<td>'+str+'<img src= '+str+'> </img></td>')
        }
        document.write('</tr>')
    }
    document.write('</table>')
}

.m File

-(IBAction) show:(id) sender{

    NSString *htmlpath = [[NSBundle mainBundle] pathForResource:@"Javascript" ofType:@"html"];
    NSString *html =[NSString stringWithContentsOfFile:htmlpath encoding:NSUTF8StringEncoding error:nil];
    NSURL *baseURl = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@",[[NSBundle mainBundle] bundlePath]] ];
    [[webView mainFrame] loadHTMLString:html baseURL:baseURl];

    NSString *jsTable=[NSString stringWithFormat:@"call('file:///Users/developer/Library/Billing/Barcodes/CO_BAT01_14.png')"];
    NSLog(@"jsTable = %@",jsTable);
    [webView stringByEvaluatingJavaScriptFromString:jsTable];
    [jsTable release];
}

I want send the image path to the .js file from my aplication. Can Anyone help me to solve my problem!!! Thanks in Advance.

Upvotes: 0

Views: 86

Answers (1)

silvansky
silvansky

Reputation: 2427

You should wait for HTML to load before executing javascript. Set the frameLoadDelegate for your WebView and implement this method:

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
    NSString *jsTable=[NSString stringWithFormat:@"call('file:///Users/developer/Library/Billing/Barcodes/CO_BAT01_14.png')"];
    NSLog(@"jsTable = %@",jsTable);
    [sender stringByEvaluatingJavaScriptFromString:jsTable];
    [jsTable release];
}

Upvotes: 1

Related Questions