【问题标题】:Swift WKWebView issue using Apple Sample Code使用 Apple 示例代码的 Swift WKWebView 问题
【发布时间】:2017-01-09 05:33:25
【问题描述】:

我才刚刚开始学习 Swift。

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

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

现在 AboutViewController 的视图由一个背景图像视图和一个关闭视图的按钮组成。

使用 WKWebView 类参考中的 Apple 示例代码

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”声明中的预期声明

  • 'override' 只能在 didReceiveMemoryWarning() 行上的类成员上指定

  • 只能声明实例方法@IBAction

我还没弄明白为什么。

需要指导,非常感谢!

【问题讨论】:

    标签: ios swift wkwebview


    【解决方案1】:

    斯威夫特3。将您的 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 1970-01-01
      • 2016-09-01
      相关资源
      最近更新 更多