views:

1798

answers:

3

Hello,
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. How would you accomplish this (I tried openURL:.)
Example:

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

Thanks,
Isaac Waller

A: 

openURL should work (that is what it is intended for!) what happened when you tried using openURL?

hhafez
Hello,I don't think that Safari will open for a data: url. When I try a http: url it will work, but Safari is probably just not registered for the data: url scheme.
Isaac Waller
That's right it isn't, why do you expect it to be registered for the data:// url scheme?
hhafez
Safari can open data: url's - it just is not registered for them. If you click on a link in a webpage to a data: url, Safari will open it. But you cannot just use openURL: :(
Isaac Waller
I see now, My answer is not much help, sorry...
hhafez
A: 

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.

GoatRider
Doesn't work - see the comments of hhafez's post.
Isaac Waller
A way to get around that might be to just display it in a UIWebView object. That's basically Safari, but entirely within your own app.
GoatRider
I need the user to be able to bookmark the page - so Safari is the only option.
Isaac Waller
+1  A: 

In iPhone OS 2.2.1, in both the simulator and on a device, opening a data: url works perfectly in a UIWebView but using openURL does precisely nothing.

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.

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. Then you could use openURL to open the 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. This seems likely to fail in the future, though. :-)

Where is the data URL coming from in the first place? 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.

ddoughty