Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Swift在iOS应用程序上通过WebView显示Html代码_Ios_Swift - Fatal编程技术网

如何通过Swift在iOS应用程序上通过WebView显示Html代码

如何通过Swift在iOS应用程序上通过WebView显示Html代码,ios,swift,Ios,Swift,我想知道如何在WebView上显示,使用字符串HTML代码,如下所示 此代码是从XML文件解析的: <htmlElement> <![CDATA[<div class='large-16 columns' style="margin-bottom: 1em;" id="vfwLetter"> <h3 class="text-center">Read the document</h3&g

我想知道如何在WebView上显示,使用字符串HTML代码,如下所示

此代码是从XML文件解析的:

<htmlElement>
                <![CDATA[<div class='large-16 columns' style="margin-bottom: 1em;" id="vfwLetter">
                <h3 class="text-center">Read the document</h3>
                <div id="DV-viewer-4501317-VFW-letter-to-to-Trump-regarding-POWs-MIAs-in" class="DC-embed DC-embed-document DV-container"></div>
                <script src="//assets.documentcloud.org/viewer/loader.js"></script>
                <script>
                    DV.load("https://www.documentcloud.org/documents/4501317-VFW-letter-to-to-Trump-regarding-POWs-MIAs-in.js", {
                    responsive: true,
                      height: 850,
                      sidebar: false,
                      pdf: false,
                      container: "#DV-viewer-4501317-VFW-letter-to-to-Trump-regarding-POWs-MIAs-in"
                    });
                </script>
                </div>]]>
</htmlElement>

阅读文件
DV.load(“https://www.documentcloud.org/documents/4501317-VFW-letter-to-to-Trump-regarding-POWs-MIAs-in.js", {
回答:是的,
身高:850,
边栏:错,
pdf:错,
容器:“DV-viewer-4501317-VFW-letter-to-to-Trump-about-POWs-MIAs-in”
});
]]>
有人知道如何用上层源代码编写代码来显示Web视图吗

或者如果你有任何其他想法,请告诉我

  • 迅捷4
  • iOS 10或更高版本

您可以这样做,首先是将html xml转换为html并创建一个空文件,然后将其另存为.html并将其加载到您的webView

获得.html文件后,可以在viewController上执行以下操作:

@IBOutlet weak var productWebView: WKWebView!

    override func viewWillAppear(_ animated: Bool) {
           super.viewWillAppear(animated)

        let htmlPath = Bundle.main.path(forResource: "sample", ofType: "html")
        let url = URL(fileURLWithPath: htmlPath!)
        let request = URLRequest(url: url)
        productWebView.load(request)

    }

我得到的答案与我的目标相符。 我使用“loadHTMLString”在WebView上显示HTML源代码

import UIKit
import WebKit

class ViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var webView: UIWebView!

    let htmlTwitterString = """
   <htmlElement><![CDATA[<html><body>
    <h1>This is a HTML document</h1>
    <p>Hello, World!!</p>
    </body></html>]]></htmlElement>
"""

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.webView.scalesPageToFit = true;
        webView.loadHTMLString(htmlTwitterString, baseURL: nil)
    }
}
导入UIKit
导入WebKit
类ViewController:UIViewController、UIWebViewDelegate{
@ibvar-webView:UIWebView!
让htmlTwitterString=“”
这是一个HTML文档
你好,世界

]]> """ 重写func viewDidLoad(){ super.viewDidLoad() //加载视图后执行任何其他设置。 self.webView.scalesPageToFit=true; loadHTMLString(htmlTwitterString,baseURL:nil) } }

谢谢你们帮我提问

是否有任何web视图url?谢谢您的评论。没有URL。这只是一个HTML脚本。你有什么想法吗?很抱歉回信迟到了。你的建议很好,但与我的目标不符。我用了“loadHTMLString”。再次感谢您的建议!