Created
December 6, 2021 15:01
-
-
Save apple-avadhesh/12200dff778717e3beb5b268cdd338f7 to your computer and use it in GitHub Desktop.
WKWebView with UIActivityIndicatorView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
import WebKit | |
class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate { | |
var loader: UIActivityIndicatorView! | |
@IBOutlet weak var webView: WKWebView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
webView.uiDelegate = self | |
webView.navigationDelegate = self | |
loader = UIActivityIndicatorView() | |
loader.center = self.view.center | |
loader.hidesWhenStopped = true | |
loader.style = UIActivityIndicatorView.Style.large | |
self.view.addSubview(loader) | |
let myURL = URL(string:"https://www.sepettte.com") | |
let myRequest = URLRequest(url: myURL!) | |
webView.load(myRequest) | |
} | |
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | |
print("didFinish") | |
showLoader(status: false) | |
} | |
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { | |
print("didStartProvisionalNavigation") | |
showLoader(status: true) | |
} | |
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { | |
print("didFail") | |
showLoader(status: false) | |
} | |
func showLoader(status: Bool) { | |
if status { | |
loader.startAnimating() | |
} else { | |
loader.stopAnimating() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment