- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须使用 UIWebView 的“LoadHTMLString”方法将 html 字符串加载到 UIWebView。
请找到下面的代码。
[wv loadHTMLString:[NSString stringWithFormat:@"<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\"><meta charset=\"utf-8\"><title>Traffic layer</title><style>html, body, #map-canvas {height: 100%%;margin: 0px;padding: 0px}</style><script src=\"https://maps.googleapis.com/maps/api/js?v=3.exp\"></script><script>function initialize() {var myLatlng = new google.maps.LatLng(34.04924594193164, -118.24104309082031);var mapOptions = {zoom: 13,center: myLatlng}var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptiotrafficLayer.setMap(map);}google.maps.event.addDomListener(window, 'load', initialize);</script></head><body><div id=\"map-canvas\"></div></body></html>"] baseURL:nil]
当我加载它时,我的 webview 是空的,没有显示任何内容。
当我将此代码放入扩展名为“html”的文件中并加载到 webview 时,它工作正常。
谁能帮我把这个加载到 WebView 上?或者任何人都可以告诉我这段代码出了什么问题。
最佳答案
你只需要创建一个html文件并将所有html内容放入其中,然后使用代码加载该文件
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"] isDirectory:NO]]];
也用这个
NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
编辑:
我被忽略了,你提到加载文件工作完美。
我认为问题是因为你在运行时加载了这个字符串。
我们都知道,当我们使用html和css文件时,我们需要在“Copy Bundle Resources”中添加这些文件。以便在运行时加载 html 页面时可用。
因此这里可能存在问题的是 html 字符串中的一些函数在加载之前使用了一些文件或帮助。这就是运行时 html 创建和加载不起作用的原因。
如果我有误导,请多多澄清。
不过,您可以在 html 文件中设置运行时值。
Exmaple.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
#latitude#
#longitude#
</body>
<html>
参见上面的文件,您可以在该文件中添加 #latitude#
和 #longitude#
标记。并在运行时替换它。就像下面的代码。
NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
html = [html stringByReplacingOccurrencesOfString:@"#latitude#" withString:<LATITUDE-VALUE>];
html = [html stringByReplacingOccurrencesOfString:@"#longitude#" withString:<LONGITUDE-VALUE>];
关于javascript - 如何使用 LoadHTMLString 方法在 WebView 中加载大型 HTML 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26799493/
关于这个有几个问题,它有一个关于嵌入在 html 字符串中的脚本的解决方案。不是我的情况。 我有一个 ios5/6 应用程序。 我尝试将加载代码放入 initWithNibName坏主意,viewDi
我是 iOS 编程新手,并试图弄清楚 loadHTMLString:baseURL: 到底做了什么,但我找不到令人满意的解释。苹果的网站只是说: Sets the main page content
我对 UIWebView 的 loadHTMLString 有一个奇怪的问题,当我使用内容 HTML 字符串调用 loadHTMLString 时,它只会显示空白 View 。 htmlstring
我正在尝试将 HTML 内容加载到 WKWebView ,它无法正确加载与屏幕高度/宽度相同的代码,相同的代码可以正常使用 UIWebView ,见如下代码 webView.loadHTMLStrin
为了解决 ios 中链接的 UITextview 蓝色问题(我想要白色的),我在自定义弹出窗口中实现了 UIWebview 在我的弹出 Controller 中,我有以下代码( uiwebview 设
我有一个 webview,我想使用 loadHtmlString 方法加载它。问题是我希望能够使用我之前下载的图像更改 img src。我还在 html 中使用 google analitics,所以
LoadHtmlString UIWebView 不工作。我想播放嵌入式你管视频。我试过使用嵌入式 HTML 字符串。但有些似乎坏了我得到白屏。我试过以下 NSString* html = @"";
我正在制作一个新闻阅读应用。我有一个用作分页 Controller 的 ArticleDetailPagingVC。这有一个带有多个 ArticleDetailViewController 的 UIS
我尝试: let body = ".. ANYTHING .." self.webView.loadHTMLString(body, baseURL: NSURL(string: "www.googl
我有一个由 UIWebView 组成的无限旋转木马。在每次滑动时,一个 UIWebView 被移除 (ARC) 并且一个被构建。在主线程上,UIWebView 通过调用 UIWebView 实例上的
我的 UIWebview 使用 loadHtmlString 加载本地 html 文件。加载的页面有指向其他本地 html 文件的链接以及使用互联网的真实链接。 我添加了一个后退按钮: if ([se
如果 Apple 在 loadHTMLString 期间遇到错误,它们会将错误抛出到单独的线程上,没有堆栈跟踪(在 Xcode4 中),并且不会输出到控制台。 您如何监听这些错误、调试它们并最终对它们
如何将以下内容转换为使用“loadRequest”而不是“loadHTMLString”? 我需要添加一个 HTTP header ,我发现的唯一方法是将 loadRequest 与 NSMutabl
我正在编写我的第一个 iPhone 应用程序.. 如何使用“loadHTMLString:baseURL:”将用户发送到名为“pagetwo.m”的 View ? 最佳答案 要从一个 View 导航到
我使用 loadHTMLString 函数通过 HTML 字符串将 HTML 代码加载到我的 UIWebView 中。这些 HTML 页面中有相同的 1.5 MB 字体文件。根据蜂窝数据使用统计数据,
我正在尝试在 UIScrollView 中延迟加载 UIWebViews。每次用户滚动时,WebViews 框架都会更新,并且应该加载新内容。而这正是我遇到麻烦的地方。重新定位效果很好,但新内容(本地
我的应用程序包中有一些嵌入的 html。我正在使用 loadHtmlString 加载它。同时我的文档文件夹中有一些图像文件。我想用我的文档文件夹中的一个替换html中的img。例如,这是我拥有的示例
我遇到了 UIWebView 的问题。我想在 UIWebView 中呈现我自己的 html 代码。 如果我使用下面的代码,它工作正常。 NSBundle *thisBundle = [NSBu
我正在开发一个mac应用程序项目。使用WKWebView的loadHTMLString方法,我能够构建一个网页 View ,除了图像加载部分之外,它运行良好。 例如: NSString *string
我不明白为什么这不起作用。我的桌面上有一个 test.htm 文件,如下所示: MathJax.Hub.Config({ tex2jax: {inlineMath: [["$","$"],
我是一名优秀的程序员,十分优秀!