为什么 UIWebView 中渲染的 HTML 文件找不到我的 CSS 和 Javascript 文件?

发布于 2024-12-07 09:34:20 字数 3155 浏览 1 评论 0原文

以下是成功将 HTML 加载到 UIWebView 中的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Player" ofType:@"html"];  
    NSData *htmlData = [NSData dataWithContentsOfFile:filePath];  
    if (htmlData) {  
        NSBundle *bundle = [NSBundle mainBundle]; 
        NSString *path = [bundle bundlePath];
        NSString *fullPath = [NSBundle pathForResource:@"Player" ofType:@"html" inDirectory:path];
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fullPath]]];
    }
}

这是 Player.html 的顶部部分,正在加载并显示 HTML 文件,仅使用零样式或 javascript 操作:

<head><title>
    Course: Pegasus 1 of each
</title><link href="Content/css/jqueryui.css" type="text/css" media="screen" rel="Stylesheet" /><link href="Content/css/Templates.css" type="text/css" media="screen" rel="Stylesheet" /><link href="Scripts/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" rel="Stylesheet" /><link href="Content/css/Pegasus.css" type="text/css" media="screen" rel="Stylesheet" />

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js" type="text/javascript"></script>
    <script src="https://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js" type="text/javascript"></script>

    <script src="Scripts/browser.js" type="text/javascript"></script>
    <script type="text/javascript" charset="utf-8" src="Scripts/jstree/jquery.tree.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" type="text/javascript"></script>

    <script type="text/javascript" src="Scripts/fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
    <script type="text/javascript" src="Scripts/fancybox/jquery.easing-1.3.pack.js"></script>
    <script type="text/javascript" src="Scripts/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
    <script type="text/javascript" src="Scripts/jquery.carouFredSel-2.5.5-packed.js"></script>
    <script type="text/javascript" src="Scripts/jquery.qtip-1.0.min.js"></script>
    <script type="text/javascript" src="Scripts/jquery.multifilterie.js"></script>
    <script type="text/javascript" src="Scripts/pegasus.js"></script>
    <script type="text/javascript" src="Scripts/jwplayer/jwplayer.js"></script>
    <script src="Scripts/jquery.form.js" type="text/javascript"></script>
    <script src="Scripts/raphael-min.js" type="text/javascript"></script>
    <script type="text/javascript" src="Scripts/pegasus.quiz.js"></script>

这是 Group 结构,请注意路径相对于 Player.html 的根位置:

在此处输入图像描述

但这就是我得到的:

在此处输入图像描述

我应该怎么做才能让它找到文件?

Here is the code that is successfully loading the HTML into UIWebView:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Player" ofType:@"html"];  
    NSData *htmlData = [NSData dataWithContentsOfFile:filePath];  
    if (htmlData) {  
        NSBundle *bundle = [NSBundle mainBundle]; 
        NSString *path = [bundle bundlePath];
        NSString *fullPath = [NSBundle pathForResource:@"Player" ofType:@"html" inDirectory:path];
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fullPath]]];
    }
}

Here is the top part of Player.html, the HTML file is being loaded and shown, just with zero styling or javascript action:

<head><title>
    Course: Pegasus 1 of each
</title><link href="Content/css/jqueryui.css" type="text/css" media="screen" rel="Stylesheet" /><link href="Content/css/Templates.css" type="text/css" media="screen" rel="Stylesheet" /><link href="Scripts/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" rel="Stylesheet" /><link href="Content/css/Pegasus.css" type="text/css" media="screen" rel="Stylesheet" />

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js" type="text/javascript"></script>
    <script src="https://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js" type="text/javascript"></script>

    <script src="Scripts/browser.js" type="text/javascript"></script>
    <script type="text/javascript" charset="utf-8" src="Scripts/jstree/jquery.tree.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" type="text/javascript"></script>

    <script type="text/javascript" src="Scripts/fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
    <script type="text/javascript" src="Scripts/fancybox/jquery.easing-1.3.pack.js"></script>
    <script type="text/javascript" src="Scripts/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
    <script type="text/javascript" src="Scripts/jquery.carouFredSel-2.5.5-packed.js"></script>
    <script type="text/javascript" src="Scripts/jquery.qtip-1.0.min.js"></script>
    <script type="text/javascript" src="Scripts/jquery.multifilterie.js"></script>
    <script type="text/javascript" src="Scripts/pegasus.js"></script>
    <script type="text/javascript" src="Scripts/jwplayer/jwplayer.js"></script>
    <script src="Scripts/jquery.form.js" type="text/javascript"></script>
    <script src="Scripts/raphael-min.js" type="text/javascript"></script>
    <script type="text/javascript" src="Scripts/pegasus.quiz.js"></script>

Here is the Group structure, notice that the paths are relative to the root location of Player.html:

enter image description here

But this is what I am getting:

enter image description here

What should I do to make it find the files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御守 2024-12-14 09:34:20

您在 Xcode 中看到的组结构不是文件夹结构。这是组织项目工作区的一种方式。
复制捆绑包资源构建阶段,文件将被复制到应用程序捆绑包的根目录。

检查 ~/Library/Apllication Support/IPhone Simulator/4.xx/Applications/uid here/your.app 中的资源目录。您将看到没有 Scripts 文件夹。

类似的东西

<script src="./browser.js" type="text/javascript"></script>

应该有效。


如果您确实需要在应用程序包中拥有文件夹结构,请在添加脚本内容时选择“为任何添加的文件夹创建文件夹引用” code> 文件夹到您的项目资源。

The group structure you see in Xcode is not a folder structure. It's a way to organize your project workspace.
During the Copy Bundle Resources build phase, the files are copied to the root of your app bundle.

Check your resource dir in ~/Library/Apllication Support/IPhone Simulator/4.x.x/Applications/uid here/your.app. You will see that there is no Scripts folder.

Something like

<script src="./browser.js" type="text/javascript"></script>

should work.


If you really need to have a folder structure in your app bundle, select "Create Folder References for any added folders" when you add the Scripts and Content folders to your project resources.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文