UIWebView delegate method shouldStartLoadWithRequest: equivalent in WKWebView?

前端 未结 7 937
忘掉有多难
忘掉有多难 2020-12-12 14:37

I have a module inside my iOS 7+ app which is a UIWebView. The html page loads a javascript that creates custom-shaped buttons (using the Raphaeljs library). With UIWebView,

7条回答
  •  情深已故
    2020-12-12 15:02

    You can add Observer for your WKWebView

     static void* keyValueObservingContext = &keyValueObservingContext;
    
    [webView addObserver:self forKeyPath:@"URL" options:0 context:keyValueObservingContext];
    
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
     {
    
    if ([keyPath isEqualToString:@"URL"]){
    // do something
      }
    }
    

    Don't forget to remove it in viewWillDisappear

    -(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [webView removeObserver:self forKeyPath:@"URL"];
    }
    

提交回复
热议问题