这是一个 Swift 错误吗?在 webView 中打开文件 [英] Is this a Swift bug? Opening a File in a webView

查看:24
本文介绍了这是一个 Swift 错误吗?在 webView 中打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.com/watch?v=Rva9ylPHi2w) 在根 viewController 中构建 webView 并加载网页.此示例适用于外部网页,但不适用于本地文件.尝试打开本地文件时出现空白屏幕.

I took a simple example on youtube (https://www.youtube.com/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆