为什么UIWebView出现&的问题? URL 中的字符(wants &)
我在 iPhone 应用程序中使用的 UIWebView 有问题。 UIWebView 中显示的源代码除许多其他内容外还包含以下标记:
<object width="150" height="121"><param name="movie" value="http://www.youtube.com/v/SOMEVIDEO&hl=de&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SOMEVIDEO&hl=de&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="150" height="121"></embed></object>
UIWebView 拒绝以正确的方式显示视频,因为嵌入的 URL 中存在 '&'-sign对象:http://www.youtube.com/v/SOMEVIDEO&hl =de&fs=1
如果我替换“&”使用“&”,一切都会像 UIWebView 中应有的那样显示。同样的问题不仅发生在嵌入视频上,也发生在常规 URL 上。奇怪的是,Safari 应用程序正确显示相同的网站,但我的应用程序中的 UIWebView 不正确。我在这里遗漏了一些明显的东西吗?
PS:当尝试使用内容呈现时,UIWebView 在页面顶部显示一条错误消息:
This page contains the following errors:
Error on line 102 at column 97: Entity Ref: expecting ';'
Below is a rendering of the page up to the first error.
更新为清楚起见:
我使用此请求来加载内容:
[myWebView loadRequest:request];
这就是为什么不可能在 NSString 上使用 -stringByAddingPercentEscapes 方法,就像 Jasarien 建议的那样。这是关于整个源,而不是单个 URL。
I have a problem with a UIWebView, that I use in my iPhone app. The source code shown in the UIWebView, contains the following markup besides many other things:
<object width="150" height="121"><param name="movie" value="http://www.youtube.com/v/SOMEVIDEO&hl=de&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/SOMEVIDEO&hl=de&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="150" height="121"></embed></object>
The UIWebView refuses to display the video the right way, because of the '&'-sign in the URL of the embedded object: http://www.youtube.com/v/SOMEVIDEO&hl=de&fs=1
If I replace the '&' with '&', everything shows like it should in the UIWebView. The same problem does not only occur at embedded videos, but also at regular URLs. The strange thing is, that the Safari-App displays the same website correctly, but not the UIWebView in my App. Am I missing something obvious here?
PS: The UIWebView shows an error-message on the top of the page, when trying to render with contents:
This page contains the following errors:
Error on line 102 at column 97: Entity Ref: expecting ';'
Below is a rendering of the page up to the first error.
Update for clarity:
I use this request to load the content:
[myWebView loadRequest:request];
That's why it is not possible to use -stringByAddingPercentEscapes method on NSString, like Jasarien suggested. It's about the whole source, not single URLs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 & 符号必须经过 URL 编码才能在 URL 中使用。对于空格和其他类型的字符也是如此。
查看
NSString
上的-stringByAddingPercentEscapes
方法。This is because ampersands must be URL encoded to work in URLs. Same goes for spaces and other types of characters.
Check out the
-stringByAddingPercentEscapes
method onNSString
.