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:
- 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?
Worded on iOS8 ~ iOS11.
Objective-C
Swift 4
!!!!NOTE!!!! THIS METHOD IS DECLARED IN WebKit Open Resource, not a public method。
This code compiles, but I have not tested it...
First I subclass
WKWebView
to overridebackForwardList
with my own subclass ofWKBackForwardList
.Then, in my
WKBackForwardList
subclass, I can either overridebackItem
&forwardItem
to make them return nil, instead of having them look into their respective list (which is most probably the default implementation).Or I can override
backList
&forwardList
in the same way I did inWKWebView
withbackForwardList
. I do this to add a setter, which will allow me remove items from the lists.Reading in the documentation you can get WKBackForwardList for the WKWebView. After that you can find the first item in the backList of WKBackForwardList and after that to use:
func go(to: WKBackForwardListItem) -> WKNavigation?
And with this you can clean the history up to the first url.