试图让 webView 加载 2 个 URLS
Trying to get webView to load 2 URLS after another
我试图让它在一个完成加载后加载 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) {
}
您必须使用 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
之后检查是否加载完成
但是这有一些问题,你必须按两次按钮,我想你不会想要的。你现在拥有它的方式不会在你的评论告诉我的雅虎之前加载 google 。如果你这样做它开始加载 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()
这样你首先加载 google。你等到它完成加载。
然后你加载雅虎并等待。
然后开始通知。
我试图让它在一个完成加载后加载 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) {
}
您必须使用 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
之后检查是否加载完成
但是这有一些问题,你必须按两次按钮,我想你不会想要的。你现在拥有它的方式不会在你的评论告诉我的雅虎之前加载 google 。如果你这样做它开始加载 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()
这样你首先加载 google。你等到它完成加载。 然后你加载雅虎并等待。 然后开始通知。