本文介绍了如何清除WKWebView的WKBackForwardList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WKWebView的 backForwardList 似乎是只读的,但我看到人们有一些非常神奇的东西可以解决这个问题。我需要找出一些清除WKWebView历史的方法。任何想法我怎么可能这样?到目前为止,我尝试了一些失败的技巧:

It appears that the backForwardList of a WKWebView is readonly, but I've seen people so some pretty magical things to get around this. I need to figure out some way of clearing the history of a WKWebView. Any ideas how I might so this? So far I've tries a few tricks that have failed:


  • 使用keyValue:forKey无效。

  • 使用C指针 - > 无效。

  • using keyValue:forKey didn't work.
  • using a C pointer -> didnt work.

我已经看到人们谈论合成属性和扩展课程,但我真的不知道它是如何工作的,无法弄明白。还有其他想法吗?

I've seen people talk about synthesizing the property and extending the class but I don't really know how that works and couldn't figure it out. Any other ideas?

推荐答案

此代码编译,但我还没有测试过...

This code compiles, but I have not tested it...

首先,我使用我自己的子类 WKWebView 覆盖 backForwardList > WKBackForwardList 。

First I subclass WKWebView to override backForwardList with my own subclass of WKBackForwardList.

然后,在我的 WKBackForwardList 子类中,我可以覆盖 backItem & forwardItem 使它们返回nil,而不是让它们查看各自的列表(这很可能是默认实现)。

Then, in my WKBackForwardList subclass, I can either override backItem & forwardItem to make them return nil, instead of having them look into their respective list (which is most probably the default implementation).

或者我可以覆盖 backList & forwardList 与我在 WKWebView 中使用 backForwardList 的方式相同。我这样做是为了添加一个setter,它允许我从列表中删除项目。

Or I can override backList & forwardList in the same way I did in WKWebView with backForwardList. I do this to add a setter, which will allow me remove items from the lists.

import Foundation
import WebKit

class WebViewHistory: WKBackForwardList {

    /* Solution 1: return nil, discarding what is in backList & forwardList */

    override var backItem: WKBackForwardListItem? {
        return nil
    }

    override var forwardItem: WKBackForwardListItem? {
        return nil
    }

    /* Solution 2: override backList and forwardList to add a setter */

    var myBackList = [WKBackForwardListItem]()

    override var backList: [WKBackForwardListItem] {
        get {
            return myBackList
        }
        set(list) {
            myBackList = list
        }
    }

    func clearBackList() {
        backList.removeAll()
    }
}

class WebView: WKWebView {

    var history: WebViewHistory

    override var backForwardList: WebViewHistory {
        return history
    }

    init(frame: CGRect, configuration: WKWebViewConfiguration, history: WebViewHistory) {
        self.history = history
        super.init(frame: frame, configuration: configuration)
    }

    /* Not sure about the best way to handle this part, it was just required for the code to compile... */

    required init?(coder: NSCoder) {

        if let history = coder.decodeObject(forKey: "history") as? WebViewHistory {
            self.history = history
        }
        else {
            history = WebViewHistory()
        }

        super.init(coder: coder)
    }

    override func encode(with aCoder: NSCoder) {
        super.encode(with: aCoder)
        aCoder.encode(history, forKey: "history")
    }
}

这篇关于如何清除WKWebView的WKBackForwardList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 21:34