为什么 UIWebView 中渲染的 HTML 文件找不到我的 CSS 和 Javascript 文件?
以下是成功将 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:
But this is what I am getting:
What should I do to make it find the files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在 Xcode 中看到的组结构不是文件夹结构。这是组织项目工作区的一种方式。
在复制捆绑包资源构建阶段,文件将被复制到应用程序捆绑包的根目录。
检查
~/Library/Apllication Support/IPhone Simulator/4.xx/Applications/uid here/your.app
中的资源目录。您将看到没有Scripts
文件夹。类似的东西
应该有效。
如果您确实需要在应用程序包中拥有文件夹结构,请在添加
脚本
和内容时选择“为任何添加的文件夹创建文件夹引用” 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 noScripts
folder.Something like
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
andContent
folders to your project resources.