我有一个定期重新加载的UIWebView。 WiFi和Cellular均已启用。 如果我手动在WiFi或Cellular之间切换,则webView不会重新加载网页。 如果设备只有一个连接(Wifi或蜂窝网络),则不会重新加载。
有没有一种方法可以定期停止UIWebView的重新加载?
class ViewController: UIViewController {
// MARK: IBOutlets
private(set) var uuid = UIDevice.current.identifierForVendor?.uuidString ?? ""
lazy private var stringUrl = "https://someLink"
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
backButtonView.alpha = 0
NotificationCenter.default.addObserver(self, selector: #selector(statusManager), name: .flagsChanged, object: Network.reachability)
self.webView.navigationDelegate = self
self.webView.scrollView.delegate = self
updateUserInterface()
}
// MARK: IBActions
@objc private func statusManager(_ notification: Notification) {
updateUserInterface()
}
// MARK: Private methods
private func updateUserInterface() {
guard let status = Network.reachability?.status else { return }
if status == .unreachable {
self.webView.isHidden = true
self.noInternetConnectionView.isHidden = false
}
}
private func loadRequest() {
configureLoadingView(state: true)
let request = URLRequest(url: URL(string: stringUrl)!)
self.webView.load(request)
self.webView.allowsBackForwardNavigationGestures = true
}
private func configureLoadingView(state: Bool) {
UIView.animate(withDuration: 0.2) {
self.loadingView.alpha = state ? 1 : 0
}
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if let stringUrl = webView.url?.absoluteString { self.stringUrl = stringUrl }
self.webView.isHidden = false
configureLoadingView(state: false)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
if let url = webView.url, let stringUrl = webView.url?.absoluteString {
if stringUrl.contains("some.com") {
webView.stopLoading()
UIApplication.shared.open(url)
}
}
}
}