gpt4 book ai didi

ios - 使用 WKWebView 显示本地 XML 文档

转载 作者:行者123 更新时间:2023-11-29 11:54:26 33 4
gpt4 key购买 nike

我正在尝试使用 WKWebView 显示 XML 文件。有没有人设法显示它?我已经设置了一个完整的示例应用程序(见下文)。该结果是在iOS 9.3 iPhone 4S模拟器上测试得到的。

LoadFileUrl()

string targetFilepath = Init("simplexsl.xml");
Init("simple.xsl");

NSUrl url = new NSUrl("file:///" + targetFilepath, false);
NSUrl readAccessUrl = new NSUrl("file:///" + Path.GetDirectoryName(targetFilepath), true);
webview.LoadFileUrl(url, readAccessUrl);

结果:白页

LoadHtmlString()

string targetFilepath = Init("simplexsl.xml");
Init("simple.xsl");

NSData documentData = NSData.FromFile(targetFilepath);

if(documentData != null && documentData.Length > 0)
{
NSString htmlString = NSString.FromData(documentData, NSStringEncoding.UTF8);
webview.LoadHtmlString(htmlString, readAccessUrl);
}

结果:显示了 xml 的内容(没有来自 xml 的标记),但未应用样式。

NSUrl readAccessUrl = new NSUrl("file:///" + Path.GetDirectoryName(targetFilepath), true);

NSString htmlString = new NSString(File.ReadAllText(targetFilepath));
webview.LoadHtmlString(htmlString, readAccessUrl);

结果:显示了 xml 的内容(没有来自 xml 的标记),但未应用样式。

LoadData()

string targetFilepath = Init("simplexsl.xml");
Init("simple.xsl");

NSUrl readAccessUrl = new NSUrl("file:///" + Path.GetDirectoryName(targetFilepath), true);

NSData documentData = NSData.FromFile(targetFilepath);

if(documentData != null && documentData.Length > 0)
{
webview.LoadData(documentData, "text/xml", "utf-8", readAccessUrl);
}

结果:白页

LoadRequest()

string targetFilepath = Init("simplexsl.xml");
Init("simple.xsl");

NSUrl url = new NSUrl("file:///" + targetFilepath, false);
webview.LoadRequest(new NSUrlRequest(url));

结果:白页

不同的路径

var documents = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.User) [0].Path;
var caches = Path.Combine (documents, "Caches");

结果:白页

var caches = Path.Combine(System.IO.Path.GetTempPath(), "www");
Directory.CreateDirectory(caches);

结果:白页

捆绑资源

string folder = NSBundle.MainBundle.BundlePath;
string source = Path.Combine(folder, "simplexsl.xml");
webview.LoadFileUrl(new NSUrl(source,false), new NSUrl(folder, true))

结果:白页

链接

现在我加载了一个带有链接的 html

<a href="simplexsl.xml">XML Link</a>

结果:加载html页面,点击链接进入白页,如果xsl在xml中被注释掉,则显示的xml没有样式

在线

webview.LoadRequest(new NSUrlRequest(new NSUrl("http://www.w3schools.com/xml/simplexsl.xml")));

结果:WKWebView 可以显示 XML file如果您允许它加载非 https 资源。

没有 XSL

注释掉 simplexsl.xml 中的以下行

<?xml-stylesheet type="text/xsl" href="simple.xsl" ?>

和使用

webview.LoadFileUrl(url, readAccessUrl);

结果:显示了 xml 的内容(没有来自 xml 的标记),但未应用样式。所以似乎存在 basepath 问题,但仅限于 XSL 文件?

当我使用 LoadFileUrl() 时,显示一个相对引用图片的 html 文件确实有效。但是对于 xml 文件,似乎存在问题。对于一个简短的测试,UIWebView 似乎可以更好地处理这个问题。

那么它是如何正确完成的呢?


基本设置

public override void ViewDidLoad()
{
base.ViewDidLoad();

var webview = new WKWebView(View.Bounds, new WKWebViewConfiguration());
View.AddSubview(webview);

// code here
}

private string Init(string filename)
{
string sourceFilepath = Path.Combine(NSBundle.MainBundle.BundlePath, filename);
var library = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).Replace("Documents", "Library");
var caches = Path.Combine(library, "Caches");
var targetFilepath = Path.Combine(caches, filename);

if (CopyFile(sourceFilepath, targetFilepath))
return targetFilepath;

return string.Empty;
}

private bool CopyFile(string sourceFilepath, string targetFilepath)
{
if (File.Exists(sourceFilepath))
{
File.Copy(sourceFilepath, targetFilepath, true);
return true;
}
return false;
}

targetFilepath 类似于 /Users/some-user/Library/Developer/CoreSimulator/Devices/8808B80D-D232-4599-B776-139409C9DDB8/data/Containers/Data/Application/BC84CFD0-A805-417C-912F-C2B07834C822/Library/Caches/simplexsl.xml

simplexsl.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="simple.xsl" ?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>

simple.xsl

<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="breakfast_menu/food">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold">
<xsl:value-of select="name"/> -
</span>
<xsl:value-of select="price"/>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:value-of select="description"/>
<span style="font-style:italic">
(<xsl:value-of select="calories"/> calories per serving)
</span>
</p>
</div>
</xsl:for-each>
</body>
</html>

如果您在 Objective-C 或 Swift 中发布您的解决方案,那将没有问题。

最佳答案

我有种不好的预感,出于安全考虑,本地文件(尽管在同一目录中)不允许进行 XSL 转换。如果我在 Chrome 中打开文件,我得到

Unsafe attempt to load URL file:///C:/some/path/Projects/TestWKWebView/TestWKWebView/Resources/simple.xsl from frame with URL file:///C:/some/path/Projects/TestWKWebView/TestWKWebView/Resources/simplexsl.xml. 'file:' URLs are treated as unique security origins.

Firefox 工作正常。在边缘我得到

XSLT8690: The system cannot locate the object specified.

来自 here 的非常有趣的评论:

Interesting fact: Safari on Mac (8.0) works just fine with local XSLT, but Safari on iOS (8) simulator (and I suppose on iOS as well) I got the same error like in Chrome "Domains, protocols and ports must match.". But if I'm to implement UIWebView in the app and load XML with XSLT everything works OK.

经过一番研究,似乎有以下解决方案:

  • 使用UIWebView
  • 使用本地网络服务器(例如查看 here )
  • 也许自己在 libxslt 的帮助下完成 XSLT(参见 herehere ,也可作为 NuGet package 获得)

关于ios - 使用 WKWebView 显示本地 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39726278/

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