试图让webView加载2个URL

时间:2017-01-26 11:49:40

标签: swift webview uiwebview uiwebviewdelegate

我试图在加载完成后加载2个webview 然后它将转到下一个并开始加载那个,如果它们都被加载,它将发送通知。

之后出错
        @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) {

}

2 个答案:

答案 0 :(得分:1)

您必须使用UIWebViewDelegate webViewDidFinishLoad。在请求完成时调用该方法。

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)
}

答案 1 :(得分:0)

所以如果我理解你现在拥有的是

  • 检查是否正在加载webview

  • 第一次按下按钮时不应该加载所以加载yahoo

  • 第二次按下按钮时会看到webview加载,因此加载了google.com

  • 之后检查是否已完成加载。

但有一些问题是,你必须按两次按钮,我认为你不想要。你现在拥有它的方式并没有在你的评论告诉我的雅虎之前加载google。如果你这样做,它开始加载谷歌,而雅虎仍然可以加载。 我会这样做:

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()

这样你首先加载谷歌。你等到装完了。 然后你加载雅虎并等待。 然后你开始通知。