2013-08-29 58 views
0

我目前有成功加載html文件的網頁視圖。我有一個確實的方法,這看起來像:使用本地iOS應用程序調用Javascript函數使用變量

- (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文件我的樣子:

<!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> 

這工作,因爲圖像顯示在網頁視圖。如果我將document.write從函數中取出,並將其直接放置在腳本標記中,則文本將出現在web視圖中。但是,我需要能夠調用允許文本顯示在webview中的函數(從我的Objective C方法),最終我還需要將此函數傳遞給一些變量。目前,我甚至無法弄清楚如何讓JavaScript函數被調用。

我看了看這個例子中,Can you call a javascript function from native code (not in a callback) using PhoneGap and iOS?,並嘗試添加以下行到我的目標C法:

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

但這並不調用函數...或者至少文本沒有出現在webview。
我是一個相當新手的程序員,對JavaScript根本不瞭解 - 任何人都可以突出顯示我做錯了什麼?

回答

0

我找不到調用一個javascript函數的方法,這個函數是在我加載到我的UIWebView的html文檔中定義的(我不知道這是,還是不可能)。但是我發現如果我不使用單獨的html文件,而是直接在我的目標c代碼中創建的html字符串,我可以實現我的目標。我能夠將變量傳遞給創建html字符串的函數,並且我能夠在html字符串中運行我的JavaScript。

這裏是我創建的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; 
} 

這裏是我怎麼裝成web視圖:

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

不知道這是要做到這一點的最好辦法,但它適用於我的目的。