gpt4 book ai didi

swift - 如何在WKWebview中使按钮刷新页面

转载 作者:行者123 更新时间:2023-11-30 10:35:02 28 4
gpt4 key购买 nike

我希望按钮“可点击”,以便刷新 WKWebview

    let buttonFrame = CGRect(x: 50, y: 50, width: 50, height: 40)
let button = UIButton(frame: buttonFrame)
button.backgroundColor = UIColor.black
webView.scrollView.addSubview(button)

最佳答案

import WebKit
import UIKit

class ViewController: UIViewController {

var webView: WKWebView!
let config = WKWebViewConfiguration()
let contentController = WKUserContentController()
var originalURL: URL!

var count = 0


override func viewDidLoad() {
super.viewDidLoad()

if let script = generateCountButtonScript() {
contentController.addUserScript(script)
}

if let script = generateAddCSSScript() {
contentController.addUserScript(script)
}

contentController.add(self, name: "count")

config.userContentController = contentController
setupWebView(config: config, url:"https://app.salonized.com/franchise/reports/overview")
}

func generateCountButtonScript() -> WKUserScript? {
guard let scriptPath = Bundle.main.path(forResource: "script", ofType: "js"),
let scriptSource = try? String(contentsOfFile: scriptPath) else {
return nil
}

let script = WKUserScript(source: scriptSource, injectionTime: .atDocumentEnd, forMainFrameOnly: true)

return script
}

func generateAddCSSScript() -> WKUserScript? {
guard let cssPath = Bundle.main.path(forResource: "style", ofType: "css"),
let cssString = try? String(contentsOfFile: cssPath).components(separatedBy: .newlines).joined() else {
return nil
}



let source = """
var style = document.createElement('style');
style.innerHTML = '\(cssString)';
document.head.appendChild(style);
"""

let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
return script
}

func setupWebView(config: WKWebViewConfiguration, url: String) {
webView = WKWebView(frame: .zero, configuration: config)
view.addSubview(webView)

let layoutGuide = view.safeAreaLayoutGuide
webView.translatesAutoresizingMaskIntoConstraints = false
webView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
webView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor).isActive = true
webView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true

if let url = URL(string: url) {
self.originalURL = url
webView.load(URLRequest(url: url))

let refresh = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView.reload))
toolbarItems = [refresh]
navigationController?.isToolbarHidden = false

//button
let buttonFrame = CGRect(x: 50, y: 50, width: 50, height: 40)
let button = UIButton(frame: buttonFrame)
button.backgroundColor = UIColor.black
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
webView.scrollView.addSubview(button)

let buttonFrame2 = CGRect(x: 120, y: 50, width: 50, height: 40)
let button2 = UIButton(frame: buttonFrame2)
button2.backgroundColor = UIColor.red
button2.addTarget(self, action: #selector(buttonAction2), for: .touchUpInside)
webView.scrollView.addSubview(button2)


}// in that way button will be scroling with webView.
}

@objc func buttonAction(sender: UIButton!) {
if let url = webView.url {
webView.reload()
} else {
webView.load(URLRequest(url: originalURL))
}
}

@objc func buttonAction2(sender: UIButton!) {
webView.load(URLRequest(url: originalURL))
}

}



extension ViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "count", let messageBody = message.body as? String {
print(messageBody)
count += 1
let javascript = "document.getElementById(\"countLabel\").innerHTML = \"Count: \(count)\";"
webView.evaluateJavaScript(javascript, completionHandler: nil)



}
}
}

关于swift - 如何在WKWebview中使按钮刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58217342/

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