Cannot subclass WKWebView

寵の児 提交于 2019-12-03 17:27:09

This is totes possible. You must only use convenience initializers and properties with default values set:

import WebKit
class MPWebView : WKWebView {
    var transparent: Bool = false

    convenience init(config: WKWebViewConfiguration = WKWebViewConfiguration()) {
       let prefs = WKPreferences()
       prefs.plugInsEnabled = true // NPAPI for Flash, Java, Hangouts
       prefs.minimumFontSize = 14
       prefs.javaScriptCanOpenWindowsAutomatically = true;
       config.preferences = prefs
       config.suppressesIncrementalRendering = false
       self.init(frame: CGRectZero, configuration: config)
    }

    convenience required init(url: NSURL) {
       self.init(config: nil)
       loadRequest(NSURLRequest(URL: url))
    }
}

Try taking out the extra decorations:

import WebKit

class TSWebView : WKWebView {

    let s: String
    let i: Int

    init(s: String, i: Int) {
        self.s = s
        self.i = i
        super.init(frame: CGRectZero, configuration: WKWebViewConfiguration())
    }

    convenience init!(coder: NSCoder!) {
        super.init(coder:coder)
    }
}

Although I'm guessing the whole point of the "availablity(*, unavailable)" is to make it so that you can't invoke the initializer (and hence can't effectively subclass WKWebView.

Just override the regular initialization like this. This worked for me, Swift 5.

override init(frame: CGRect, configuration: WKWebViewConfiguration) {
    super.init(frame: frame, configuration: configuration)
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!