无法添加本地html+;iphonewebview中的css文件

无法添加本地html+;iphonewebview中的css文件,iphone,css,uiwebview,Iphone,Css,Uiwebview,我正在通过以下代码加载本地html页面: NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"title" ofType:@"html"]; NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile]; [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL

我正在通过以下代码加载本地html页面:

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"title" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
…它装载得非常好。我添加了link标签来加载本地css文件,该文件也添加到了项目中,但它确实按照预期加载了页面

<head>
<link media="only screen and (max-device-width: 480px)" href="/style.css" type= "text/css" rel="stylesheet" />
</head>

但是当我从服务器加载带有css的本地html页面时,它的加载效果非常好

<head>
<link href="http://somehitng.com/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>


有人能告诉我我做错了什么吗?

您需要将web视图的基本URL设置为HTML文件所在的位置,以便本地链接引用可以工作

NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];

您需要将web视图的基本URL设置为HTML文件所在的位置,以便本地链接引用可以工作

NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];

它应该是NSURL*baseURL=[NSURL fileURLWithPath:[[NSBundle mainBundle]resourcePath]];哎呀,那是个愚蠢的错误。我编辑了答案来解决这个问题。它应该是NSURL*baseURL=[NSURL fileURLWithPath:[[NSBundle mainBundle]resourcePath];哎呀,那是个愚蠢的错误。我编辑了答案来解决这个问题。