我制作了这个webview,我希望在使用应用程序时没有互联网连接或丢失互联网连接时显示错误消息。
这是我的代码:
import UIKit
class FirstViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var webView1: UIWebView!
var refreshControl:UIRefreshControl?
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
webView1.delegate = self
let url = URL(string: "https://pharmacyuni.blogspot.com")
webView1.loadRequest(URLRequest(url: url!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func goBack(_ sender: Any) {
webView1.goBack()
}
func webViewDidStartLoad(_ webView: UIWebView)
{
activityIndicator.startAnimating()
}
func webViewDidFinishLoad(_ webView: UIWebView)
{
activityIndicator.stopAnimating()
}
@IBAction func refreshButton(_ sender: Any) {
webView1.reload()
}
}
答案 0 :(得分:1)
您可以使用委托方法:
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
print("webview did fail load with error: \(error)"
let message: String = error.localizedDescription
let alert = UIAlertController(title: "something", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default) { action in
// use action here
})
self.present(alert, animated: true)
}