uiactivity and uiwebview delegate



我的项目中有一个 uiactivity 视图。 我想在页面加载时开始制作动画,并在 Web 视图最终加载时停止并隐藏。 这在我的情况下不起作用。

我尝试使用UIWebViewDelegate,但它已被弃用。

class WebviewViewController: UIViewController {
@IBOutlet var webView: UIWebView!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
let disposeBag = DisposeBag()
var specURL:String?
override func viewDidLoad() {
super.viewDidLoad()
configureUIActivityView()
_ = loadWebview()
.subscribe(onSuccess: { url in
DispatchQueue.main.async {
self.webView.loadRequest(URLRequest(url: url))
self.activityIndicator.stopAnimating()
self.activityIndicator.isHidden = true
}
})
}
func loadWebview() -> Single<URL> {
return Single<URL>.create { single in
guard let url = URL(string: self.specURL!.trimmingCharacters(in: .whitespacesAndNewlines)) else {
assertionFailure("no view available")
//single(.error(NetworkError.noImage))
return Disposables.create {}
}
single(.success(url))
return Disposables.create {}
}
}
}

最好使用 WKWebView 来实现,因为正如您提到的,UIWebViewDelegate 已被弃用。

下面是一个使用 KVO 监控 WKWebView 加载状态的简单示例。

import WebKit
class ViewController: UIViewController, WKUIDelegate {
private var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
webView.addObserver(self, forKeyPath: "loading", options: .new, context: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "http://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let keyPath = keyPath, let change = change else { return }
switch keyPath {
case "loading":
if let loading = change[.newKey] as? Bool {
print("Is webview loading: (loading)")
}
default:
break
}
}
deinit {
webView.removeObserver(self, forKeyPath: "loading")
}
}

相关内容

  • 没有找到相关文章

最新更新