2010-03-24 26 views
1

我爲應用程序生成一個HTML文件,並且在這個HTML文件中有一個指向樣式表的鏈接,一個指向圖像。對生成的HTML使用圖像和css文件UIWebView

這裏是我試過到目前爲止:

NSMutableString *toReturn = [NSMutableString stringWithCapacity:100]; 
NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"etapes.css" ofType:nil]; 

[toReturn appendFormat:@"<html><head><title></title><link href=\"%@\" media=\"all\" rel=\"stylesheet\" type=\"text/css\" /></head><body>", cssPath]; 

它產生正確的完整路徑正確的文件,這是正常的,如果我想訪問我的Mac上,但在模擬器上或我的iPhone它並沒有指出正確的地方...

你有什麼想法我怎麼能做到這一點?

感謝

回答

4

我發現this tutorial而回。儘管如此,我最終只使用<風格>標記。

NSString *path = [[NSBundle mainBundle] bundlePath]; 
NSURL *baseURL = [NSURL fileURLWithPath:path]; 
[webView loadHTMLString:htmlString baseURL:baseURL]; 

這樣做的另一種方法是,像這樣:

-(NSString *)urlForFileNamed:(NSString *) name ofType:(NSString *) type { 
    NSString *filePath = [[NSBundle mainBundle] pathForResource: name ofType: type]; 
    if (!filePath) { NSLog(@"No path found for file %@.%@", name, type); } 
    return filePath; 
} 

-(void)viewDidLoad { 
    [super viewDidLoad]; 

    NSString *path = [[NSBundle mainBundle] bundlePath]; 
    baseUrl = [NSURL fileURLWithPath: path]; 

    [view loadHTMLString: [self html] baseURL: [NSURL fileURLWithPath: path]]; 
} 

-(NSString *)html { 
    // Obviously the below's just a stub for proper HTML 
    return [NSString stringWithFormat: @"<img src=\"@\" />", [self urlForFileNamed: @"foo" ofType: @"png"]]; 
} 
+0

這是正確的。爲您的包使用本地URL,然後在HTML中使用相對路徑。你的樣式表的url只是'etapes.css' – 2010-03-24 19:26:02

+0

我必須指定一個baseURL!非常感謝你的工作! – TomShreds 2010-03-24 19:39:05

+0

首先爲我工作。謝謝 – 2014-01-23 10:19:27

1

您可以嘗試編輯線

NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"etapes.css" ofType:nil]; 

NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"etapes" ofType:@"css"];