gpt4 book ai didi

html - UIWebView 中带有 HTML 和 CSS 的 iPhone native 应用程序

转载 作者:行者123 更新时间:2023-11-28 17:36:36 25 4
gpt4 key购买 nike

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

在我的元素中,我像这样在 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" />

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

最佳答案

首先,删除 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”文件夹而不是基本文件夹。

关于html - UIWebView 中带有 HTML 和 CSS 的 iPhone native 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24990357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com