ios - UIWebView 中本地 HTML 文件中的 HTML 链接

标签 ios ios7 uiwebview

这里是 XCode 的新手

基本上,我们有一个依赖于 UIWebView 的应用程序。当有有效的网络连接时,我们将移动网页加载到 UIWebView 中,如果没有连接,我们将加载本地版本的 html 网页。我的场景与离线或有限网络连接场景有关。当这种离线情况确实发生时,我可以使用此线程的答案很好地加载我的本地 html 文件:

Load resources from relative path using local html in uiwebview

在加载的本地 html 文件中单击一个简单的 html 链接(也在我的本地应用程序目录中)时,我的问题就出现了。我已经尝试过这些,但是当我点击链接时,没有任何反应,链接不会带我到任何地方,模拟器只允许我复制文本:

<a href="file:///Sample.html">
<a href="file://Sample.html">
<a href="Sample.html">
<a href="/Sample.html">
<a href="Testing/Sample.html">

sample.html 网页与初始加载的 html 页面位于同一目录中。不确定我哪里出错了,显然遗漏了一些简单的东西。

最佳答案

我建议您重新检查您的目录层次结构。您描述的行为是当您尝试导航到的链接在本地设备上不存在时发生的情况。 我运行了以下小测试:

将 Webview 添加到 View Controller 并加载 page1.html

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 10, 320, 300)];
[self.view addSubview:webView];

NSURL *url = [[NSBundle mainBundle] URLForResource:@"page1" withExtension:@"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseUrl];

page1.html

<html>
<body>
    <h1>Page 1</h1>
    <a href="page2.html">Go to Page2</a>
</body>
</html>

page2.html

<html>
    <body>
        <h1>Page 2</h1>
        <a href="page1.html">Back to Page 1</a>
    </body>
</html>

项目结构图

Image of the project structure

最终结果是,它有效。单击会将您从第 1 页带到第 2 页,然后再返回,所有这些都使用本地文件。如果我们将链接更改为:

<a href="page3.html"></a>

这不存在,你会得到与你正在经历的相同的行为。也就是说,您只能剪切和复制,而不能实际转到链接。

enter image description here enter image description here

关于ios - UIWebView 中本地 HTML 文件中的 HTML 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25043880/

相关文章:

ios - 如何在ios中创建日期滚动条

ios - 如何为两个不同的选择器 View 调用两个不同的数组(swift4)

ios - 更改通过 ActivityView 打开的电子邮件对话框的色调

ios - UIActivityViewController 在使用 sourceView 或 barButtonItem 的 iPad 上崩溃

ios - iPad 上的 UIActivityViewController 奇怪行为,App 未优化

ios - 以编程方式将图像位置放置在按钮顶部

ios7 - iOS 7 UITextView : Size of nstextattachment getting 2x after reopening the application

ios - iOS 7 和 iOS 6 中 UIPickerview 选择器值的默认字体名称和大小是多少

javascript - stringByEvaluatingJavaScriptFromString,查找特定的输入标签

ios - 点击手势错误,在错误的 View 中识别点击