• 一个小问题


    我的浏览器在搭建好基本UI后运行还算正常。但添加了自动旋转代码后,开始报错。即使关闭Xcode,在模拟器上独立运行app,也只能坚持不到一分钟就闪退。

    错误代码:

    ***Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<ViewController: 0x7f877a71a1a0>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
    Key path: estimatedProgress
    Observed object: <WKWebView: 0x7f877a5c41c0; frame = (0 0; 667 375); gestureRecognizers = <NSArray: 0x7f877a5e1e30>; layer = <CALayer: 0x7f877a5c3b70>>
      Change: {
      kind = 1;
      new = "0.6176357155547082";
      }
      Context: 0x0'
      ***First throw call stack:
      (
      0   CoreFoundation                      0x000000010b7a4c65 __exceptionPreprocess + 165
      1   libobjc.A.dylib                     0x000000010b43dbb7 objc_exception_throw + 45
      2   CoreFoundation                      0x000000010b7a4b9d +[NSException raise:format:] + 205
      3   Foundation                          0x000000010b06acaf -[NSObject(NSKeyValueObserving)observeValueForKeyPath:ofObject:change:context:] + 73
      4   NewBrowserProject                   0x000000010aefa246 -[ViewController observeValueForKeyPath:ofObject:change:context:] + 1030
      5   Foundation                          0x000000010af9ad96 NSKeyValueNotifyObserver + 356
      6   Foundation                          0x000000010af99fbd NSKeyValueDidChange + 466
      7   Foundation                          0x000000010af9e6df -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 118
      8   WebKit                              0x000000010d2c47d1 _ZN6WebKit13PageLoadState13commitChangesEv + 1133
      9   WebKit                              0x000000010d227dc6 _ZN6WebKit12WebPageProxy17didChangeProgressEd + 74
      10  WebKit                              0x000000010d23a96c _ZN6WebKit12WebPageProxy17didReceiveMessageEPN3IPC10ConnectionERNS1_14MessageDecoderE + 2976
      11  WebKit                              0x000000010d12df58 _ZN3IPC18MessageReceiverMap15dispatchMessageEPNS_10ConnectionERNS_14MessageDecoderE + 120
      12  WebKit                              0x000000010d26cd9c _ZN6WebKit15WebProcessProxy17didReceiveMessageEPN3IPC10ConnectionERNS1_14MessageDecoderE + 24
      13  WebKit                              0x000000010d0b5baa _ZN3IPC10Connection15dispatchMessageENSt3__110unique_ptrINS_14MessageDecoderENS1_14default_deleteIS3_EEEE + 94
      14  WebKit                              0x000000010d0b85c4 _ZN3IPC10Connection18dispatchOneMessageEv + 110
      15  JavaScriptCore                      0x00000001153c1436 _ZN3WTF7RunLoop11performWorkEv + 454
      16  JavaScriptCore                      0x00000001153c1cea _ZN3WTF7RunLoop11performWorkEPv + 26
      17  CoreFoundation                      0x000000010b6d8431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
      18  CoreFoundation                      0x000000010b6ce2fd __CFRunLoopDoSources0 + 269
      19  CoreFoundation                      0x000000010b6cd934 __CFRunLoopRun + 868
      20  CoreFoundation                      0x000000010b6cd366 CFRunLoopRunSpecific + 470
      21  GraphicsServices                    0x0000000110264a3e GSEventRunModal + 161
      22  UIKit                               0x000000010bddf8c0 UIApplicationMain + 1282
      23  NewBrowserProject                   0x000000010af00aaf main + 111
      24  libdyld.dylib                       0x000000010e5fb145 start + 1
      25  ???                                 0x0000000000000001 0x0 + 1
     )
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb)
    

    看来是上一篇提到的添加进度条的代码出现了问题。
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

      if ([keyPath isEqualToString:@"estimatedProgress"] && object == self.webView) {
           [self.loadingProgress setAlpha:1.0f];
           [self.loadingProgress setProgress:self.webView.estimatedProgress animated:YES];
           if(self.webView.estimatedProgress >= 1.0f) {
                      [UIView animateWithDuration:0.3 
                                            delay:0.3
                                         options:UIViewAnimationOptionCurveEaseOut
                                       animations:^ {
                             [self.loadingProgress setAlpha:0.0f];
                             }
                                       completion:^(BOOL finished) {
                             [self.loadingProgress setProgress:0.0f animated:NO];
                             }
                      ];
          }
     }else {
           [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
     }
    }
    

    这段代码在WKWebView的estimatedProgress属性发生变化时,显示UIProgressView控件,并当estimatedProgress属性为1时消失。

    我的代码问题在于:设备每次旋转后,app都会移除WKWebView控件,并重新添加新的WKWebView控件。其estimatedProgress属性出现了从1到0的情况,对于这个观察事件来讲却无法作出反应,因为我将UIProgressView控件添加到了WKWebView控件上,随之移除并重新生成。

    解决办法是,不再对WKWebView做 移除并重新生成 的老办法,而是直接设置setAutoresizingMask:属性

  • 相关阅读:
    (转)移动端实现垂直居中的几种方法
    FloatingActionButton 完全解析
    android 开源编辑器
    如何使用PullToRefresh
    Android 使用代码主动去调用控件的点击事件(模拟人手去触摸控件)
    Gradle 下载
    android studio 中移除module和恢复module
    android studio 使用jar包,arr包和怎么使用githup开源项目中的aar包或module
    DrawerLayout 和 NavigationView 的使用
    android studio 集成微信登录
  • 原文地址:https://www.cnblogs.com/rim99/p/5009887.html
Copyright © 2020-2023  润新知