这是一个 Swift 错误吗?在 webView 中打开文件

编程入门 行业动态 更新时间:2024-10-26 16:30:59
本文介绍了这是一个 Swift 错误吗?在 webView 中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在开始学习这种我非常喜欢的新语言......但它似乎比我预期的要复杂.

I am starting working my way through this new language, which I like a lot ... but it seems to to be buggier than I expected.

我在 youtube 上举了一个简单的例子(https://www.youtube/watch?v=Rva9ylPHi2w) 在根 viewController 中构建 webView 并加载网页.此示例适用于外部网页,但不适用于本地文件.尝试打开本地文件时出现空白屏幕.

I took a simple example on youtube (https://www.youtube/watch?v=Rva9ylPHi2w) of building a webView in the root viewController and loading a webpage. This example works fine with an external webpage, but not a local file. I get a blank screen when trying to open a local file.

以下代码用于 ViewController.swift 文件.

The code below is for the ViewController.swift file.

import UIKit

class ViewController: UIViewController {

    @IBOutlet var WebView : UIWebView

    var URLPath = ""/files/test.html""

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        loadAddressURL()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func loadAddressURL() {
        let requestURL = NSURL (string:URLPath)
        let request = NSURLRequest(URL: requestURL)

        println("the requestURL is: \(requestURL)")

        WebView.loadRequest(request)
    }

}

你可以看到我添加了一个 println 来显示正在使用的 URL 请求并且它向后显示:

You can see I added a println to show the URL request being used and it appears backward:

使用本地文件,我得到requestURL 是:/files/test.html",这是我放置示例 html 文件的位置.

With a local file I get "the requestURL is: /files/test.html", which is where I have the sample html file located.

他们是我做错了什么还是我应该向 Apple 提交这个文件?

Is their something I did wrong or should I file this with Apple?

推荐答案

这就是我归档以从本地文件加载 html 的方式

This is how i archived to load html from local file

 let bundle = NSBundle.mainBundle()
 var url = bundle.URLForResource("yourHTML", withExtension: "html");
 var request = NSURLRequest(URL: url!);
 webContent.loadRequest(request)

这篇关于这是一个 Swift 错误吗?在 webView 中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 04:21:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1129046.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:这是一个   错误   文件   Swift   webView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!