使用Apple示例代码的Swift WKWebView问题

时间:2017-01-09 05:33:25

标签: ios swift wkwebview

我才开始学习Swift。

按照教程,AboutViewController用于设置视图以显示应用程序的信息。

本教程使用UIWebView处理提供内容的html文件。但官方文档建议使用WKWebView。

现在,AboutViewController的视图包含一个背景图像视图和一个用于关闭视图的按钮。

使用WKWebView的类引用中的Apple Sample Code

import UIKit
import WebKit

class AboutViewController: UIViewController, WKUIDelegate {

    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self

        view = webView

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        if let filePath = Bundle.main.url(forResource: "info", withExtension: "html") {
            if let htmlData = try? Data(contentsOf: filePath) {
                let baseURL = URL(fileURLWithPath: Bundle.main.bundlePath)
                webView.load(htmlData,
                             mimeType: "text/html",
                             characterEncodingName: "UTF-8",
                             baseURL: baseURL)
            }
        }
    }

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

    @IBAction func close() {
        dismiss(animated: true, completion: nil)
    }

}

Xcode显示以下三个错误

  • Swift编译器错误:“AboutViewController”声明中的预期声明

  • '覆盖'只能在didReceiveMemoryWarning()

  • 行上的类成员上指定
  • 只能声明实例方法@IBAction

我还是没弄明白为什么。

需要指导,非常感谢!

1 个答案:

答案 0 :(得分:0)

Swift3。将您的html文件(此处为test.html)删除到您的项目中。

// If needed disable nabber and grey background
    WebView.scrollView.bounces = false
    WebView.scrollView.showsHorizontalScrollIndicator = false
    WebView.scrollView.showsVerticalScrollIndicator = false

    let url = Bundle.main.url(forResource: "test" + langue, withExtension:"html")
    let req = NSURLRequest(url: url!)
    WebView.loadRequest(req as URLRequest)