This is my code, explanation is below:
func webViewDidStartLoad(homewebview: UIWebView) {
if Reachability.isConnectedToNetwork() == true {
print("Internet connection: OK")
} else {
print("Internet connection: FAILED")
let path: String = NSBundle.mainBundle().pathForResource("file", ofType: "html")!
let url: NSURL = NSURL.fileURLWithPath(path)
let request: NSURLRequest = NSURLRequest(URL: url)
homewebview.loadRequest(request)
}
}
When clicking a hyperlink webViewDidStartLoad
will called.
When there's an internet connection it prints Internet connection: OK
.
When there's no internet connection it should print Internet connection: FAILED
and open file.html
.
But when opening the local file it calls the whole function again, and again and again…like a never ending loop.
I want to check for internet connection when clicking a (hyper)link in the WebView. If there's no internet connection a local file should load in this WebView, without calling the function again.
How can I fix it? Does anybody have an idea?
Just rise a flag when you are loading a local page to skip the recursive routine.
var loadingLoacalPage = false
//..//
func webViewDidStartLoad(homewebview: UIWebView) {
if Reachability.isConnectedToNetwork() == true {
print("Internet connection: OK")
} else {
print("Internet connection: FAILED")
let alert = UIAlertView(title: "No internet connection", message: "Check internet connection.", delegate: nil, cancelButtonTitle: "Okay")
alert.show()
if(!loadingLoacalPage){
let path: String = NSBundle.mainBundle().pathForResource("file", ofType: "html")!
let url: NSURL = NSURL.fileURLWithPath(path)
let request: NSURLRequest = NSURLRequest(URL: url)
homewebview.loadRequest(request)
loadingLoacalPage = true
}
}
}