gpt4 book ai didi

swift - 试图让 webView 加载 2 个 URLS

转载 作者:行者123 更新时间:2023-11-28 15:55:13 25 4
gpt4 key购买 nike

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

关于swift - 试图让 webView 加载 2 个 URLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41872828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com