2013-06-10 2 views
0

NSString 값을 objective-c에서 javascript로 보내고 싶습니다. 여기 코드입니다 :매개 변수를 객관적에서 자바 스크립트로 전달

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 파일

-(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]; 
} 

나는의 .js 파일에 이미지 경로를 전송합니다 내 응용에서. 누구나 내 문제를 해결하는 데 도움을 줄 수 있습니다 !!! 미리 감사드립니다.

답변

1

자바 스크립트를 실행하기 전에 HTML이로드 될 때까지 기다려야합니다. WebView에 frameLoadDelegate을 설정하고이 방법을 구현하십시오.

- (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]; 
} 
관련 문제