Reputation: 25
Im trying to make it load 2 webviews after one gets finished loading then it will go to the next one and start to load that one and if both of them are loaded it will send a notification.
It gives an error after
@IBAction func MybuttonAction(_ sender: Any) {
if !googleLoaded {
googleLoaded = true
let url = URL(string: "https://google.com")
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)
print("Google loaded")
let url1 = URL(string: "https://yahoo.com")
let requestObj1 = URLRequest(url: url1!)
webView.loadRequest(requestObj1)
print("yahoo loaded")
} else if !yahooLoaded {
yahooLoaded = true
let alertController = UIAlertController(title: "ThankYou", message: "Both Views have finished loading", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
self.aviLoadingSpinner.stopAnimating()
}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
}
Upvotes: 0
Views: 1097
Reputation: 374
You must use UIWebViewDelegate webViewDidFinishLoad. That method is called when a request is completed.
import WebKit
class yourViewController: UIViewController, UIWebViewDelegate {
@IBoutlet weak var webView: UIWebView!
var googleLoaded: Bool = false
var yahooLoaded: Bool = false
override func viewDidLoad() {
self.webView.delegate = self
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if !googleLoaded {
googleLoaded = true
let url = URL(string: "https://yahoo.com")
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)
} else if !yahooLoaded {
yahooLoaded = true
let alertController = UIAlertController(title: "ThankYou", message: "Both Views have finished loading", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
self.aviLoadingSpinner.stopAnimating()
}
}
@IBAction func MybuttonAction(_ sender: Any) {
let url = URL(string: "https://google.com")
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)
}
Upvotes: 1
Reputation: 95
So if I understand correctly what you now have is
Check if webview is loading
the first time you press a button it shouldn't be loading so load yahoo
the second time you press the button it sees the webview loading so it loads google.com
after that you check if it is finished loading.
but a few problems with this is, you have to press the button twice, which i assume you don't want. The way you have it now doesn't load google before yahoo which your comments tell me. And if you do it like this it starts loading google while yahoo COULD be still loading. I would do it like this :
let url = URL (string: "https://google.com")
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)
while webview.IsLoading{
// you should wait here
}
let url = URL (string: "https://yahoo.com")
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)
while webview.IsLoading{
//You should wait again here
}
//Now you don't have to check anymore to see if they are done
let alertController = UIAlertController(title: "ThankYou", message: "Both Views have finished loading", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
self.aviLoadingSpinner.stopAnimating()
This way you first load google. you wait until it finished loading. Then you load yahoo and wait. And after that you start the notification.
Upvotes: 0