반응형

WKWebView 이용중에 사이트 내에서 URL 변경되는 것을 감지 하기 위해서는 다음과 같이 사용하면 됩니다.

// 옵져버 설정
webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil)

...

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == #keyPath(WKWebView.url) {
        guard let url = self.webView.url?.absoluteString else { return }
        // 변경된 URL 확인
        print(url)
    }
}
반응형
Posted by 까칠코더
,