Swift 试图让webView一个接一个地加载2个URL
我试图让它加载2个网络视图后,一个得到完成加载 然后它将转到下一个并开始加载该文件,如果两个文件都已加载,它将发送通知 之后它会给出一个错误Swift 试图让webView一个接一个地加载2个URL,swift,webview,uiwebview,uiwebviewdelegate,Swift,Webview,Uiwebview,Uiwebviewdelegate,我试图让它加载2个网络视图后,一个得到完成加载 然后它将转到下一个并开始加载该文件,如果两个文件都已加载,它将发送通知 之后它会给出一个错误 @IBAction func MybuttonAction(_ sender: Any) { if !googleLoaded { googleLoaded = true let url = URL(string: "https://google.com") let request
@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) {
}
必须使用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)
}
如果我理解正确的话,你现在拥有的是 检查是否正在加载webview 第一次按下按钮时,它不应该加载,所以加载yahoo 第二次按下按钮时,它会看到webview正在加载,因此它会加载google.com 然后检查是否已完成加载 但有几个问题是,你必须按两次按钮,我想你不会想要的。你的评论告诉我,你现在的方式并没有在雅虎之前加载谷歌。如果你这样做,它会在雅虎还在加载的时候开始加载谷歌。 我会这样做:
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()
通过这种方式,您首先加载google。您要等到它完成加载。
然后加载yahoo并等待。
然后你开始通知。好的,我输入并进行了调整,但是现在我得到一个错误,说“UIWebView”类型的值没有成员“isLoading”,嗯,我假设isLoading存在,因为你自己使用了它。你应该试试@Lucas的答案。他使用WebViewDidFinishLoad嗯,我不知道如何将其添加到代码中?比如,我怎样才能在一个完整的代码中编写它?如果googleLoaded{googleLoaded=true//request for yahoo}否则如果!yahooLoaded{yahooLoaded=true//notification}googleLoaded的值是多少?都是false,因为您将变量初始化为false,一旦第一个请求google完成,它将设置true并请求yahoo。当第二个请求完成时,它将设置为true并发送通知。希望有帮助!我怎么能把googleLoaded和yahooLoaded放到URL字符串中呢?googleLoaded和yahooLoaded是bools,允许你的代码说:好吧,它是googleLoaded?还是雅虎?必须在viewdidload中执行第一个请求,在webDidFinishLoad方法中执行另一个请求。我会更新我的回答当我按下按钮1次它进入谷歌,当谷歌完全完成加载后它会留在谷歌它不会切换到yahoo.com我怎么能让它在完成后加载另一个并在每次按下时刷新它?我佩服你的帮助!非常感谢。