Html 链接到 WebView 中的资源 - iPhone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/478665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Link to resources inside WebView - iPhone
提问by Isaac Waller
I have a webview in my iPhone application, and I also have some html files inside my Resources folder. When my app loads, I load in a page from my resources into my webview. But , I need to make links inside my webview that point to some other resources (For example, images, or other html files). Just doing a relative link doesn't work:
我的 iPhone 应用程序中有一个 webview,我的资源文件夹中还有一些 html 文件。当我的应用程序加载时,我将资源中的一个页面加载到我的 web 视图中。但是,我需要在我的 webview 中创建指向其他一些资源(例如,图像或其他 html 文件)的链接。只是做一个相对链接不起作用:
<a href="OtherPage.html">Doesn't work</a>
采纳答案by August
When you load those resources, you need to set the base URL. You can do this using the method:
当您加载这些资源时,您需要设置基本 URL。您可以使用以下方法执行此操作:
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
...where baseURL would be the file URL of your resources folder.
...其中 baseURL 将是您的资源文件夹的文件 URL。
回答by Joe D'Andrea
Apparently there's a bit of a gotcha according to the iPhone SDK Release Notes for iPhone OS 3.0:
根据iPhone OS 3.0的iPhone SDK 发行说明,显然有一些问题:
Issue:UIWebView can't load local resources in apps built against 3.0.
When using
[UIWebView loadHTMLString:baseURL:]
, the HTML string should not refer to local resources with thefile://
scheme. Instead, pass inNULL
or afile://
URL forbaseURL:
, or include the resources directly in the HTML with<style>
and<script>
tags.
问题:UIWebView 无法在针对 3.0 构建的应用程序中加载本地资源。
使用 时
[UIWebView loadHTMLString:baseURL:]
,HTML 字符串不应使用该file://
方案引用本地资源。相反,传入NULL
或 的file://
URLbaseURL:
,或将资源直接包含在带有<style>
和<script>
标签的 HTML 中 。
Fair enough. But passing in a standard-issue file://
URL for baseURL
doesn't quite work. However ... it turns out that, if you change /
to //
and spaces to %20
, you will be set! (Using %20
makes a lot of sense, but the double-slash part caught me by surprise.)
很公平。但是传递一个标准问题的file://
URLbaseURL
并不完全有效。但是……事实证明,如果您更改/
为//
并将空格更改为%20
,您将被设置!(使用%20
很有意义,但双斜线部分让我感到惊讶。)
Let's say you already have NSString *markup
set up. Here's all you do. (I've wrapped the code here for readability. You may wish to refactor/adjust to taste.)
假设您已经NSString *markup
设置好了。这就是你所做的一切。(为了便于阅读,我将代码包装在这里。您可能希望重构/调整以适应口味。)
NSString *resourcePath = [[[[NSBundle mainBundle] resourcePath]
stringByReplacingOccurrencesOfString:@"/" withString:@"//"]
stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
[webView loadHTMLString:markup baseURL:[NSURL URLWithString:
[NSString stringWithFormat:@"file:/%@//", resourcePath]]];
So long as your CSS, JavaScript and images are referred to by filename alone, this should do the trick in iPhone OS 3.0 where loadHTMLString:baseURL:
is concerned!
只要您的 CSS、JavaScript 和图像仅通过文件名引用,这在 iPhone OS 3.0 中loadHTMLString:baseURL:
就可以解决问题!
回答by Isaac Waller
I fixed it: I used this for the BaseURL like August said:
我修复了它:我将它用于 BaseURL 就像 August 说的:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
Thank you!
谢谢!
回答by Chris Lundie
This code would return a string with the URL of a file named "OtherPage.html" in your bundle's resources directory.
此代码将返回一个字符串,其中包含包资源目录中名为“OtherPage.html”的文件的 URL。
[[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"OtherPage" ofType:@"html"]] absoluteString]
回答by Michael Gaylord
This worked for me. My HTML files were in sub-folders (not groups) in my project. My html page is called page01 and the sub-folder is html:
这对我有用。我的 HTML 文件位于我项目的子文件夹(不是组)中。我的 html 页面被称为 page01,子文件夹是 html:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *pathToHtml = @"html";
NSURL *baseURL = [[NSURL alloc] initFileURLWithPath:pathToHtml isDirectory:YES];
NSString *html = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"page01" ofType:@"html"
inDirectory:@"html"]
encoding:NSUTF8StringEncoding error:nil];
[webview loadHTMLString:html baseURL:baseURL];
回答by Ed Marty
Try loading OtherPage.html first. If you can't then it's not there, and you've missed some part of adding it to the project. If you can, then there may just be a typo in the link, or the baseURL could be incorrect, as stated by August. When I created an html file with images in it that were in the resource file, it worked fine just using
首先尝试加载 OtherPage.html。如果你不能,那么它就不存在,并且你错过了将它添加到项目的某些部分。如果可以,那么链接中可能只是拼写错误,或者 baseURL 可能不正确,如 August 所述。当我创建了一个 html 文件,其中包含资源文件中的图像时,只需使用它就可以正常工作
<img src="file.png">
回答by Isaac Waller
Maybe it does have something to do with the baseurl: when I load the resources, I use this line:
也许它确实与 baseurl 有关:当我加载资源时,我使用这一行:
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
see at the end, it says baseURL: "". If I take this out, it doesn't work (crashes), but I don't know the path to my resources. If somebody does.....?
看到最后,它说 baseURL: ""。如果我把它拿出来,它就不起作用(崩溃),但我不知道我的资源的路径。如果有人.....?
回答by ceejayoz
Take a look at how Phonegap does it. Here's their tutorial on local resources on the iPhone. Step 11 might be what you're neglecting.
看看Phonegap是如何做到的。这是他们关于 iPhone 本地资源的教程。第 11 步可能是您忽略的。
回答by kolyuchiy
For me the problem was that js files were not included in the bundle. Be sure to check that.
对我来说,问题是 js 文件没有包含在包中。一定要检查一下。
回答by Howard Pautz
OK, here 3.5 years later :) ... tried Michael Gaylord's suggestion above exactly, but it didn't work in sims for 5.0, 5.1, 6.0, 6.1 - nor in real devices running same. Howeverthis SO clarification on where resources are bundled ( Where is build output going?) helped make it work for me like this:
好的,3.5 年后在这里 :) ... 完全尝试了上面迈克尔盖洛德的建议,但它在 5.0、5.1、6.0、6.1 的模拟游戏中不起作用 - 也不适用于运行相同的真实设备。然而,这个关于资源捆绑在哪里的 SO 澄清(构建输出去哪里?)帮助它像这样对我有用:
- create a Finder folder in the project ( as Michael suggests - and I also did NOT try 'group')
- put your html in that folder using Finder
- in Xcode use file-> add files to project
Make sure the project recognizes that it should be in the bundle by:
- Right click on main project target in Navigatorwindow,
- make sure TARGETS is selected, the Build Phasestab,
- Click the Copy Bundle Resourcestriangle thingy |>
- And - here's what's different from Michael's- It should already show your html file AND the path. (Perhaps the XCoders have improved the add files AI :)
- 在项目中创建一个 Finder 文件夹(正如迈克尔所建议的 - 我也没有尝试“组”)
- 使用 Finder 将您的 html 放在该文件夹中
- 在 Xcode 中使用file-> add files to project
通过以下方式确保项目识别它应该在包中:
- 右键单击导航器窗口中的主项目目标,
- 确保选择了目标,构建阶段选项卡,
- 单击Copy Bundle Resources三角形 thingy |>
- 并且 -这与 Michael 的不同- 它应该已经显示了您的 html 文件和路径。(也许 XCoders 改进了添加文件 AI :)
So all I had to do was copy - paste Michael's code above verbatumand change this one line:
所以我所要做的就是复制 - 将迈克尔的代码粘贴到逐字上面并更改这一行:
inDirectory:@"html"]
to:
到:
inDirectory:nil]
and Shaazam! it worked :)
和沙赞!有效 :)