I've set up KVO notification to watch some properties of a UIWebView like so
[webView addObserver:self
forKeyPath:@"canGoBack"
options:NSKeyValueObservingOptionNew
context:NULL];
and have
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
but it never gets called. Am I missing something or is UIWebView just not observable?
canGoBack
is a readonly
property: in order for it to be KVO compliant it would have to redeclare that property as readwrite
in its implementation and then set the property via a synthesised setter. I suspect that instead canGoBack
is just set via its ivar, which would not send a notification via the KVO system:
[self setCanGoBack:YES]; // Would notify KVO observers (as long as any reimplementation of automaticallyNotifiesObserversForKey does place restrictions)
_canGoBack = YES; // Would not notify KVO observers
This related question discusses the issue in detail: Is it possible to observe a readonly property of an object in Cocoa Touch?
As a workaround, you could set a UIWebViewDelegate
and check the value of [UIWebView canGoBack]
in [UIWebViewDelegate webViewDidFinishLoad:]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With