2010-03-24 24 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"];