【问题标题】:Trying to get webView to load 2 URLS after another试图让 webView 一个接一个地加载 2 个 URL
【发布时间】:2017-01-26 11:49:39
【问题描述】:

我试图让它在一个完成加载后加载 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) {

}

【问题讨论】:

    标签: swift webview uiwebview uiwebviewdelegate


    【解决方案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)
    }
    

    【讨论】:

    • 嗯,我不确定如何将其添加到代码中?就像我怎样才能用完整的代码写出来? if !googleLoaded { googleLoaded = true //对 yahoo 的请求 } else if !yahooLoaded { yahooLoaded = true //通知 } googleLoaded 的值是多少?
    • 两者都是假的,因为您将变量初始化为假,一旦第一个请求(谷歌)完成,它就会设置为真并请求雅虎。当第二个请求(雅虎)完成时,它设置为 true 并发送通知。希望对您有所帮助!
    • 如何将 googleLoaded 和 yahooLoaded 放到 URL 字符串中?
    • googleLoaded 和 yahooLoaded 是布尔值,允许您的代码说:好的,它的 googleLoaded?还是它的雅虎?您必须在 viewdidload 中执行第一个请求,在 webDidFinishLoad 方法中执行另一个请求。我会更新我的答案
    • 当我按下按钮 1 次时它会进入谷歌,当谷歌完成加载时它会停留在谷歌它不会切换到 yahoo.com 我怎样才能让它在完成后加载另一个并刷新它每次按下?我很佩服你的帮助!谢谢
    【解决方案2】:

    所以如果我理解正确的话,你现在拥有的是

    • 检查 webview 是否正在加载

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

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

    • 然后检查它是否完成加载。

    但有一些问题是,您必须按两次按钮,我认为您不希望这样做。你现在拥有它的方式不会在你的 cmets 告诉我的雅虎之前加载谷歌。如果你这样做,它会开始加载谷歌,而雅虎可能仍在加载。 我会这样做:

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

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

    【讨论】:

    • 好的,所以我输入并进行了调整,但我现在得到错误提示“UIWebView”类型的值没有成员“isLoading”
    • 嗯,我认为 isLoading 存在是因为您自己使用了它。您应该尝试@Lucas 的回答。他使用webViewDidFinishLoad
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多