【问题标题】:HTML Link from within a local HTML file in a UIWebViewUIWebView 中本地 HTML 文件中的 HTML 链接
【发布时间】:2014-09-22 12:16:36
【问题描述】:

这里对 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 页面位于同一目录中。不知道我哪里出错了,显然缺少一些简单的东西。

【问题讨论】:

  • 其实webpage中的html在服务端有自己的目录结构。因此,如果您单击网页中的链接(在线模式),则会引用服务器目录中的该链接。在离线模式下,您刚刚下载了页面的 HTML 内容,但您无权访问该服务器目录以实际点击链接。明白了吗?
  • 我想我明白你在说什么。但我已将服务器内容和页面拉到我的本地目录中。然后我不能参考我的 sample.html 页面,因为它也在我的本地目录中(在应用程序本身中)?
  • 现在这是实际问题,伙计!尝试在 iOS 模拟器的 Safari 浏览器中访问相同的内容并分享结果
  • 我稍微编辑了一下,让它更明显
  • 您能否在您的问题中添加一些代码,这些代码可能显示链接周围的部分,以及您如何设置 webview url。

标签: ios ios7 uiwebview


【解决方案1】:

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

向视图控制器添加 Webview 并加载 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>

项目结构图片

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

&lt;a href="page3.html"&gt;&lt;/a&gt;

这不存在,你会得到你正在经历的同样的行为。那就是你只能剪切和复制,而不能真正去链接。

【讨论】:

  • 谢谢你的榜样!它对我帮助很大:) 我试图有一个基类来控制在互联网连接的情况下(以及在没有互联网连接的情况下)页面的导航位置,但我很难找到获取它的方法识别这些本地 html 页面,再次感谢!
  • 看来方法“loadHTMLString”似乎不配合:) let urlString = Bundle.main.url(forResource: "Page1", withExtension: "html") webView.loadRequest(URLRequest( url:urlString!)) 虽然这似乎可行。
【解决方案2】:

要使用来自本地文件系统的资源,请尝试此解决方案,就像我在 WebView 上显示图像时所做的那样。

-(NSString *)imagePath:(NSString *)fileName
{
    if ([fileName isEqualToString:@""] == NO) {
        NSLog(@"%@",fileName);
        NSString *ImagePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"png"];
        ImagePath = [ ImagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
        ImagePath = [ ImagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
        ImagePath = [@"file://" stringByAppendingString:ImagePath];
        return ImagePath;
    }

    else
    {
        return @"";
    }
}

假设您想返回保存 HTML 页面的完整路径。

【讨论】:

    【解决方案3】:

    @Chris 有一个很好的例子,可能我们需要更新如下:

        let webView  = UIWebView(frame: self.view.frame)
        webView.delegate = self
        self.view.addSubview(webView)
    
        let  urlString = Bundle.main.url(forResource: "Page1", withExtension: "html")
    
        webView.loadRequest(URLRequest(url:urlString!))
    

    【讨论】:

      猜你喜欢
      • 2018-07-20
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多