2012-02-21 95 views
2

我想用一個HTML文件在我的代碼目前我使用的代碼下面一行:如何使用HTML文件中的iPhone

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"htm" inDirectory:nil]; 
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
[WebView loadHTMLString:htmlString baseURL:nil]; 

但我現在面臨的問題是:在HTML頁面包含一些圖像和所有圖像位於項目的文件夾中,但我無法在運行時在HTML頁面中看到該圖像。

+0

路徑更改爲圖片到你的HTML文件。 – beryllium 2012-02-21 10:12:12

+0

是的,我做到了這一點,但它不工作 – user1173142 2012-02-21 10:17:04

+0

你需要轉換NSData數據 – 2012-02-21 10:26:16

回答

3

這裏使用HTML到項目的可能途徑,爲負載HTML文件到Xcode項目

例1,從URLNSURL加載內容

NSURL *url = [NSURL URLWithString:@"http://google.com"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [webView loadRequest:request]; 

例2,裝載 下面的代碼從字符串

NSString *HTMLData = @"<h1>Hello this is a test</h1>"; 
    [webView loadHTMLString:HTMLData baseURL:nil]; 

實施例3,裝載從本地文件的內容的內容

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"html"]; 
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile]; 
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]]; 

希望這會幫助代碼開發應用

4

您可以設置基本URL到你的資源文件夾,其中您存放圖片:

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"htm" inDirectory:nil]; 
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
NSString *resourcePath = [[NSBundle mainBundle]resourcePath]; 
NSURL *baseUrl = [[NSURL alloc]initFileURLWithPath:resourcePath]; 
[self.myWebView loadHTMLString:htlmString baseURL:baseUrl];