繁体   English   中英

如何在 WKWebView 中使用 URL 加载 CSS 文件

[英]How to load CSS file by using URL in WKWebView

提示:本站提供中英文对照查看,鼠标放在中文字句上可显示英文原文。 若本文未解决您的问题,推荐您尝试使用帮您解决。

我想通过使用 URL 链接注入CSS

在职的

在代码下面工作,如果我将 CSS 文件存储在项目的Bundle路径中,

    lazy var webView: WKWebView = {
    guard let path = Bundle.main.path(forResource: "style", ofType: "css") else {
        return WKWebView()
    }

    let cssString = try! String(contentsOfFile: path).components(separatedBy: .newlines).joined()
    let source = """
    var style = document.createElement('style');
    style.innerHTML = '\(cssString)';
    document.head.appendChild(style);
    """
    let preferences = WKPreferences()
    preferences.setValue(true, forKey:"developerExtrasEnabled")
    let userScript = WKUserScript(source: source,
                                  injectionTime: .atDocumentEnd,
                                  forMainFrameOnly: true)

    let userContentController = WKUserContentController()
    userContentController.addUserScript(userScript)

    let configuration = WKWebViewConfiguration()
    configuration.userContentController = userContentController
    configuration.preferences = preferences

    let webView = WKWebView(frame: .zero,
                            configuration: configuration)
    webView.navigationDelegate = self
    webView.scrollView.isScrollEnabled = false
    webView.scrollView.bounces = false
    return webView
}()

期待

我有 CSS 文件,它存储在我们的服务器中,有一些路径链接可以说,

https://xyz/styles/style.css

所以我想通过使用 URL 链接应用style.css文件,

Please help me to apply CSS file by using URL only, I don't want to store it in bundle, bundle CSS file is already working for me, our CSS style will change dynamically so I want apply URL link CSS file.

您可以像下载任何其他文件类型一样下载远程 css 文件。

这是一个简单的例子(注意:只是一个例子!!! -不打算成为生产代码:):

func gotTheCSS(_ css: String) -> Void {
    print(css)
    // here is where you would inject it, just like you did when
    //  loading it from the bundle
}

override func viewDidLoad() {
    super.viewDidLoad()

    DispatchQueue.global().async {
        if let url = URL(string: "http://yoursite.com/css/style.css") {

            do {
                let cssString = try String(contentsOf: url, encoding: String.Encoding.utf8)
                self.gotTheCSS(cssString)
            }
            catch {
                print(error)
            }

        }
    }
}

感谢人们对我的问题的帮助/建议/答案。

我找到了解决方案,通过使用 Z2C56C3605804D854E48C608D47Z 的Z2C56C3605804D854E48C608D47Z文件应用 CSS 文件

使用 CSS 文件的后端服务器链接,例如,

    let cssURL "https://xyz/styles/style.css" // Use your server css file url link

然后使用 JavaScript 注入问题,所以,我已经将 JavaScript 注入代码样式更改为如下代码所示链接,我在“覆盖 func viewDidAppear(_ animated: Bool)”方法中编写的所有代码,只需复制粘贴代码,它就可以为您工作出色地。

            let source = """
            var link = document.createElement('link');
            link.href = '\(url)';
            link.rel= 'stylesheet'
            document.head.appendChild(link);
            """
            let userScript = WKUserScript(source: source,
                                          injectionTime: .atDocumentEnd,
                                          forMainFrameOnly: true)

            let userContentController = WKUserContentController()
            userContentController.addUserScript(userScript)

            let configuration = WKWebViewConfiguration()
            configuration.userContentController = userContentController

            self.webView = WKWebView(frame: self.documentDiscriptionView.bounds,
                                     configuration: configuration)
            self.webView.navigationDelegate = self
            self.webView.scrollView.isScrollEnabled = false
            self.webView.scrollView.bounces = false

            self.webView.isOpaque = false
            self.webView.backgroundColor = UIColor.clear
            self.webView.scrollView.backgroundColor = UIColor.clear

            self.self.webViewContainerView.addSubview( self.webView)

    ///// Set Constraint
            self.webView.translatesAutoresizingMaskIntoConstraints = false
            self.webView.leadingAnchor.constraint(equalTo: self.webViewContainerView.leadingAnchor, constant: 0).isActive = true
            self.webView.trailingAnchor.constraint(equalTo: self.webViewContainerView.trailingAnchor, constant: 0).isActive = true
            self.webView.topAnchor.constraint(equalTo: self.webViewContainerView.topAnchor, constant: 0).isActive = true
            self.webView.bottomAnchor.constraint(equalTo: self.webViewContainerView.bottomAnchor, constant: 0).isActive = true

我的问题现在已经解决了,希望这个解决方案能帮助有人通过使用链接申请 css,如果有人想要任何帮助申请 CSS 到 Z9F0268549B4CDC23EFC36A9670C90E2,请告诉我

问题未解决?试试使用:帮您解决问题。
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM