Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is UIWebView KVO compliant?

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?

like image 850
iain Avatar asked Nov 16 '11 01:11

iain


1 Answers

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:].

like image 51
sam-w Avatar answered Nov 15 '22 18:11

sam-w