无法在iphone webview中添加本地html + css文件

Ris*_*shi 1 css iphone 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:[NSURL URLWithString:@""]];
Run Code Online (Sandbox Code Playgroud)

......它装得很完美.我添加了链接标记来加载项目中添加的本地css文件,但它确实按预期加载页面.

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

但是当我从服务器加载带有css的本地html页面时,它会完全加载.

<head>
<link href="http://somehitng.com/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
Run Code Online (Sandbox Code Playgroud)

谁能帮助我,我做错了什么?

Sim*_*een 5

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

NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];
Run Code Online (Sandbox Code Playgroud)