2013-08-29 2 views
0

현재 HTML 파일을 성공적으로로드하는 웹보기가 있습니다.네이티브 iOS 앱의 변수로 자바 함수 호출

<!doctype html> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width,initial-scale=1"> 
    <link rel="stylesheet" href="inlineAttachmentTemplateStyling.css"> 

<script> 
function myFunction() 
    { 
    document.write("Hello from my html file!!!!"); 

    } 
</script>  

</head> 
<body id="body"> 
<div id="container"> 
    <img id="testImage" src="fauxImageAttachment2.JPG" /> 
</div><!-- #container --> 
</body> 
</html> 

이 이미지가 웹보기에 나타납니다에서 작동 :처럼 외모가

- (EmailView *)loadHTMLIntoEmailView:(EmailView *)emailView 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"inlineAttachmentTemplate" ofType:@"html"]; 
    NSData *htmlData = [NSData dataWithContentsOfFile:path]; 
    NSString *resourceURL = [[NSBundle mainBundle] resourcePath]; 
    resourceURL = [resourceURL stringByReplacingOccurrencesOfString:@"/" withString:@"//"]; 
    resourceURL = [resourceURL stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
    NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",resourceURL]]; 

    [emailView.webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:baseURL]; 
    return emailView; 
} 

HTML 파일 :처럼 나는 그가하는이 방법이 보인다. 함수에서 document.write를 가져와 스크립트 태그 안에 직접 배치하면 텍스트가 webview에 나타납니다. 그러나 텍스트를 webview에 표시 할 수있는 함수 (내 Objective C 메서드에서)를 호출 할 수 있어야하며 궁극적으로이 함수에 일부 변수를 전달해야합니다. 현재 자바 스크립트 함수를 호출하는 방법을 알지 못합니다.

나는,이 예에서 Can you call a javascript function from native code (not in a callback) using PhoneGap and iOS?을보고, 나의 목표 C 메서드에 다음 줄을 추가하는 시도 :

[emailView.webView stringByEvaluatingJavaScriptFromString:@"myFunction()"]; 

그러나이 함수를 호출하지 않습니다 ... 또는 적어도 텍스트가 표시되지 않습니다 webview.
저는 초심자 프로그래머이고 자바 스크립트에 대해 많이 알지 못합니다. 내가 잘못하고있는 것을 강조 할 수 있습니까?

답변

0

내 UIWebView로로드하는 HTML 문서의 내부에 정의 된 javascript 함수를 호출하는 방법을 찾을 수 없습니다 (이것이 가능하지 않거나 불가능한 지 알 수 없음). 그러나 나는 별도의 html 파일을 사용하지 않는다면 내 목표를 달성 할 수 있다는 것을 알았지 만 객관적인 C 코드 안에 직접 생성 된 html 문자열입니다. html 문자열을 만든 함수에 변수를 전달할 수 있었고 html 문자열에서도 자바 스크립트를 실행할 수있었습니다. 여기

는 HTML 문자열 만들기위한 나의 방법 :

- (NSString *)htmlString:(NSString *)emailMessage { 

    NSString *imageName = @"fauxImageAttachment2.JPG"; 

    NSString *string = [NSString stringWithFormat:@"" 
    "<!doctype html>" 
    "<head>" 
    "<meta charset='utf-8'>" 
    "<meta name='viewport' content='width=device-width,initial-scale=1'>" 
    "<link rel='stylesheet' href='inlineAttachmentTemplateStyling.css'>" 
    "</head>" 

    "<body id='body'>" 
    "<div id='container'>" 
    "<script type='text/javascript'>" 
    "document.write('%@');" 
    "</script>" 
    "<img id='testImage' src='%@' />" 
    "</div><!-- #container -->" 
    "</body>" 

    "</html>", emailMessage, imageName]; 

    return string; 
} 

을 그리고 여기가 웹보기로로드하는 방법입니다 :이이 작업을 수행하는 가장 좋은 방법 인 경우

- (EmailView *)loadHTMLIntoEmailView:(EmailView *)emailView 
{ 
    NSString *sampleEmailMessage = @"Hey! Check out this awesome photo I sent you!!!!!"; 
    NSString *path = [[NSBundle mainBundle] bundlePath]; 
    NSURL *baseURL = [NSURL fileURLWithPath:path]; 
    [emailView.webView loadHTMLString:[self htmlString:sampleEmailMessage] baseURL:baseURL]; 

    return emailView; 
} 

확실하지 않음 ,하지만 그것은 내 목적을 위해 일했습니다.

관련 문제