- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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
关于ios - 使用 WKWebView 显示本地 XML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39726278/
我遇到了死胡同。我只是想不出在用户关闭应用程序后恢复 WKWebView 的方法。当他重新打开它时,他访问过的所有网页都必须在那里。 有什么建议吗? 最佳答案 All the web pages he
有没有办法让 WKWebView 加载完整的 URL对于每个请求? webView:didFinishNavigation: 仅适用于 mainFrame导航并且不提供 URL 请求参数。 我如何获得
我想复制 Facebook 应用程序的行为,当您单击外部链接时,它会在新浏览器中打开(但它不在 Safari 中)。 我目前有一个加载我的网站的 WKWebView。我可以在点击事件中捕获用户并打开它
我正在尝试将所有加载的数据从一个 WKWebView 复制到一个新的 WKWebView 以便在新的 webView 加载时加载所有内容提出了。关于如何执行此操作或对此的最佳方法有何建议? 最佳答案
WKWebView 允许您通过 Safari 打开外部链接(与基本 url 不同的链接)。 但是,有时您想打开 WKWebView 本身内的所有链接. 例如,假设您有一个基本网址 "https://t
我已经发布了我使用的委托(delegate)方法,如果没有按钮操作事件,我如何使用 javascript 添加该事件。我如何在我的 View Controller 中使用功能主义者。 func web
我正在制作一个应用程序,用户可以在其中单击一个按钮以禁用所有 JavaScript 在下一个页面加载时(而不是重新启动应用程序才能生效)。我尝试了以下代码,但首选项没有保存。 JavaScript 在
我知道我们可以初始化一个 UIWebView 或 WKWebView 然后注入(inject) JavaScript 来获取 navigator.userAgent 的值,但是这需要初始化UIWebV
目标:网站加载完成后对WKWebView进行截图 采用的方法: 在 UIViewController 中定义了一个 WKWebView 变量 创建了一个名为 screen capture() 的扩展方
服务人员在 WKWebView 中可用在 iOS 11.3 测试版中,但似乎在 iOS 11.3 的最终 GM 版本中不可用。 有谁知道如何在 WKWebView 中重新启用 Service Work
我有 44 个 UIButtons,每个按钮都应该连接到带有本地 html 的 wkwebview 的 vc。 有没有办法在一个 vc 中实现所有 44 个按钮? 这是 wkwebview 示例 vc
我正在尝试在 App WebView 中实现“真正的”全屏,这意味着无论如何我都希望网页内容完全显示在屏幕下方。 这是我面临的当前情况,当将 WebView 构建到 superviews 边界时: 红
我正在尝试使用带有单个按钮的简单 WKWebView,单击该按钮后应通过 JavaScript 回调到 Swift。但是,我在 Xcode 控制台中看到以下日志,并且 View 加载了空的 WKWeb
我正在尝试使用带有单个按钮的简单 WKWebView,单击该按钮后应通过 JavaScript 回调到 Swift。但是,我在 Xcode 控制台中看到以下日志,并且 View 加载了空的 WKWeb
我正在使用一个 MacOS 应用程序,它需要使用 WKUserScript 功能将消息从网页发送回 MacOS 应用程序。我正在处理文章 https://medium.com/capital-one-
对于UIWebView,我们可以通过以下方式获取内容高度: [webView stringByEvaluatingJavaScriptFromString:@"document.body.offset
优化背景 众所周知,H5 的部分优势(开发快,迭代快,热更新)是很明显的,公司客户端的部分业务都是由 H5 来实现的,网络好的情况下体验也是很不错的 但是其实 H5 的体验是比原生
由于Xcode8发布之后,编译器开始不支持iOS 7了,这样我们的app也改为最低支持iOS 8.0,既然需要与web交互,那自然也就选择使用了 iOS 8.0之后 才推出的新控件 WKWebVie
下图中的这个“up up up up”是使用wkwebview加载的html字符串 webView?.loadHTMLString((currentLock?.text)!, baseURL: nil
我的应用程序有 1 个 ViewController,它加载一个使用 Auth0 处理登录的网络应用程序 url。在 iOS 13.3 中,webView 已停止在登录后加载其他页面。 但是,当我尝试
我是一名优秀的程序员,十分优秀!