From 36c66fd1869dd3e15a1c463ad92116aae58d7af1 Mon Sep 17 00:00:00 2001 From: noeyiz Date: Sun, 10 Mar 2024 01:12:04 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20WebView=EC=97=90=20progressBar=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iBox/Sources/Web/WebView.swift | 62 ++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/iBox/Sources/Web/WebView.swift b/iBox/Sources/Web/WebView.swift index bfcb41e..ff164b3 100644 --- a/iBox/Sources/Web/WebView.swift +++ b/iBox/Sources/Web/WebView.swift @@ -11,17 +11,30 @@ import WebKit import SnapKit class WebView: UIView { + + private var progressObserver: NSKeyValueObservation? + var selectedWebsite: URL? { didSet { loadWebsite() } } + // MARK: - UI Components + private let webView = WKWebView().then { $0.isOpaque = false $0.scrollView.contentInsetAdjustmentBehavior = .always } + private let progressView = UIProgressView().then { + $0.progressViewStyle = .bar + $0.tintColor = .label + $0.sizeToFit() + } + + // MARK: - Initializer + override init(frame: CGRect) { super.init(frame: frame) setupProperty() @@ -34,6 +47,7 @@ class WebView: UIView { } deinit { + progressObserver?.invalidate() webView.stopLoading() webView.navigationDelegate = nil webView.scrollView.delegate = nil @@ -44,16 +58,25 @@ class WebView: UIView { private func setupProperty() { backgroundColor = .backgroundColor webView.navigationDelegate = self + progressObserver = webView.observe(\.estimatedProgress, options: .new) { [weak self] webView, _ in + self?.progressView.setProgress(Float(webView.estimatedProgress), animated: true) + } } private func setupHierarchy() { addSubview(webView) + addSubview(progressView) } private func setupLayout() { webView.snp.makeConstraints { make in make.edges.equalToSuperview() } + + progressView.snp.makeConstraints { make in + make.bottom.leading.trailing.equalToSuperview() + make.height.equalTo(2) + } } private func loadWebsite() { @@ -65,19 +88,28 @@ class WebView: UIView { } extension WebView: WKNavigationDelegate { -// func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { -// print("웹뷰 로딩 실패: \(error.localizedDescription)") -// } -// -// func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { -// print("웹뷰 프로비저널 네비게이션 실패: \(error.localizedDescription)") -// } -// -// func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { -// if let url = navigationAction.request.url { -// print("웹뷰가 리다이렉트 되는 URL: \(url.absoluteString)") -// } -// -// decisionHandler(.allow) -// } + + func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { + progressView.setProgress(1.0, animated: true) + // 약간의 딜레이를 주어서 프로그레스 바가 완전히 차도록 함 + DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { + self.progressView.isHidden = true + } + } + + // func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { + // print("웹뷰 로딩 실패: \(error.localizedDescription)") + // } + // + // func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { + // print("웹뷰 프로비저널 네비게이션 실패: \(error.localizedDescription)") + // } + // + // func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { + // if let url = navigationAction.request.url { + // print("웹뷰가 리다이렉트 되는 URL: \(url.absoluteString)") + // } + // + // decisionHandler(.allow) + // } }