Swift Playgrounds WKWebView 中的 HTML 中的双引号?

问题描述 投票:0回答:1

如何在 Swift Playgrounds(iPad) WKWebView 中使用带有双引号 ("") 的 HTML?

我尝试在引号前使用 \ 来做到这一点: 请注意,这是在游乐场而不是应用程序中完成的。

import UIKit
import WebKit
import PlaygroundSupport

let myURL = URL(string:"https://www.apple.com")

class WebViewController: UIViewController {
    override func loadView() {
        let config = WKWebViewConfiguration()
        let webView = WKWebView(frame: .zero, configuration: config)
        self.view = webView
    }
    
    var webView: WKWebView { self.view as! WKWebView }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.webView.loadHTMLString("<iframe frameborder=\”0\” src=\”https://itch.io/embed/2292016\” width=\”552\” height=\”167\”><a href=\”https://v205.itch.io/ghost-battle\”>Ghost Battle by V205</a></iframe>", baseURL: nil)
    }
}

let vc = WebViewController()
PlaygroundPage.current.liveView = vc

但它给出了一个错误:

Invalid escape sequence in literal

这是我尝试使用的 HTML:

<iframe frameborder="0" src="https://itch.io/embed/2292016" width="552" height="167"><a href="https://v205.itch.io/ghost-battle">Ghost Battle by V205</a></iframe>

谢谢!

swiftui wkwebview swift-playground
1个回答
0
投票

您可以使用此处概述的方法:如何在“”内打印双引号?

例如,要使用扩展分隔符,请用数字符号 # 将字符串括起来。

除了双引号“,你的字符串还包含”,需要替换为“

最后示例中的行变为:

self.webView.loadHTMLString(#"<iframe frameborder="0" src="https://itch.io/embed/2292016" width="552" height="167"><a href="https://v205.itch.io/ghost-battle">Ghost Battle by V205</a></iframe>"#, baseURL: nil)
© www.soinside.com 2019 - 2024. All rights reserved.