【问题标题】:iPhone native app with HTML and CSS in UIWebView在 UIWebView 中带有 HTML 和 CSS 的 iPhone 原生应用
【发布时间】:2014-09-19 08:27:09
【问题描述】:

我已经在我的 Xcode 应用程序包中添加了我的 Html、CSS 和图像文件。

在我的项目中,我像这样在 UIWebView 中加载了 Html 内容,

NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"html" inDirectory:nil];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];

NSURL *Url = [NSURL fileURLWithPath:htmlString];
[self.webView loadHTMLString:htmlString baseURL:Url];

Html 文件已加载,但缺少 CSS 和图像。

在我的 Html 文件中添加 CSS 和图片如下,

<link href="/css/myCss.css" type="text/css" rel="stylesheet"/>

   <img class"icon" alt="" src="/img/icon.png" />

需要帮助来解决这个问题。

【问题讨论】:

  • 尝试在地址中的第一个“/”之前添加两个点,例如。 "../css/myCss.css" .
  • @AbhinavGauniyal:刚试过。但不工作
  • 删除路径中的文件夹是否有效?这样 /css/myCss.css 就变成了 myCss.css 并且 /img/icon.png 变成了 icon.png。我相信这就是我不久前让它在我的应用程序中工作的方式,尽管这些文件位于文件夹和组中。
  • 我也试过了,结果一样
  • 哦,抱歉,刚刚检查了代码,我正在考虑的应用程序甚至没有使用 WebView :/

标签: html css iphone xcode uiwebview


【解决方案1】:

首先,去掉 html 文件路径中的初始 / ,这样你就有了:

<link href="css/myCss.css" type="text/css" rel="stylesheet"/>

<img class"icon" alt="" src="img/icon.png" />

确保在添加文件时使用“文件夹”选项:“为任何添加的文件夹创建文件夹引用”。

在 webview 中加载 html 的代码可以简化为:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myFile" ofType:@"html" inDirectory:nil];

[self.webView loadRequest:[NSURLRequest requestWithURL:url]];

我还建议将您的 html 资源放入“www”或“html”文件夹而不是基本文件夹。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多