在cordova混合开发的iOS项目中,当从原生跳转到CDVViewController类型的H5页面时,如果页面没有加载完就通过导航按钮或者利用手势返回上一层会报以下错误(测试机系统为iOS9.3):
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x135936a80 of class UIView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x135c02d20> (
<NSKeyValueObservance 0x135c01b20: Observer: 0x1358f5990, Key path: frame, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x135c019b0>
<NSKeyValueObservance 0x135c02cf0: Observer: 0x1358f5990, Key path: bounds, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x135c02730>
)'
看错误的原因是某个UIView类型的对象deallocated的时候没有remove掉KVO,根据错误里面的Key Path:frame和bounds找到是一个插件的问题:CDVSplashScreen中使用了KVO,但是返回上一层时没有remove掉
[parentView addObserver:self forKeyPath:@"frame" options:0 context:nil];
[parentView addObserver:self forKeyPath:@"bounds" options:0 context:nil];
在它的- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context方法里是根据屏幕的方向更改加载图。因为我的项目只是竖屏,所以不需要这个功能,直接把这些相关代码注释掉即可