简体   繁体   English

iPhone Open DATA:Url在Safari中

[英]iPhone Open DATA: Url In Safari

I have a Data: URL (see: http://en.wikipedia.org/wiki/Data_URI_scheme ) (as a NSString) and I want to open it in Safari. 我有一个Data:URL(参见: http//en.wikipedia.org/wiki/Data_URI_scheme )(作为NSString),我想在Safari中打开它。 How would you accomplish this (I tried openURL:.) 你会如何做到这一点(我试过openURL:。)
Example: 例:

data:text/html;base64,(Some Base64 Encoded Data Here)

In iPhone OS 2.2.1 and 5.0.1, in both the simulator and on a device, opening a data: url works perfectly in a UIWebView but using openURL does precisely nothing. 在iPhone OS 2.2.1和5.0.1中,在模拟器和设备上,打开数据: url在UIWebView完美运行,但使用openURL确实没有任何效果。

And Safari will gladly, and properly, render such an URL if you are willing to type one into the navigation bar, so this is clearly a problem with sharedApplication openURL , not with Safari. 如果您愿意在导航栏中输入一个URL,Safari很乐意并且正确地呈现这样的URL,因此这显然是sharedApplication openURL的问题,而不是Safari。

If the base64 string is short enough (less than 2K, probably) you could wrap it as a query parameter to an http URL that simply returns a redirect to the data url. 如果base64字符串足够短(可能小于2K),您可以将其作为查询参数包装到http URL,该URL只返回重定向到数据URL。 Then you could use openURL to open the http URL. 然后,您可以使用openURL打开http URL。 Yes, this means bouncing through some server, but it would work. 是的,这意味着通过一些服务器弹跳,但它会工作。

Alternatively, since Safari obviously hasn't done it, you could tell the iPhone that your app is the handler for the data: scheme and take responsibility for rendering the content in a UIWebView. 或者,由于Safari显然还没有这样做,你可以告诉iPhone 你的应用程序是data: scheme的处理程序,并负责在UIWebView中呈现内容。 This seems likely to fail in the future, though. 不过,这似乎有可能在未来失败。 :-) :-)

Where is the data URL coming from in the first place? 数据URL首先来自哪里? Perhaps you could construct a web page whose contents are nothing more than <iframe src="<the data url>"/> and again, use openURL on that URL. 也许您可以构建一个网页,其内容只不过是<iframe src="<the data url>"/>并再次使用该URL上的openURL。

This should do it: 这应该这样做:

NSURL *yourURL = [[NSURL alloc] initWithString:yourStr];    
[[UIApplication sharedApplication] openURL:yourURL];
[yourURL release];

assuming "yourStr" is an NString with the URL where your data is located. 假设“yourStr”是一个NString,其中包含数据所在的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM