0

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.

1 Answer 1

1

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];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.