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)
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.
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.
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.
Related
In my app, when I send request to server either I will get url or pdf or image in response and based on that I will display the result (url->webview, image->imageview etc).But I do not know how to identify whether the response is url or pdf or image?
Please anyone know how I can achieve this?
Thanks in advance!
I assume you do use NSURLConnection to download your file. So, implement this delegate method ...
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
... cast response to NSHTTPURLResponse and get Content-Type value from allHeaderFields.
But it depends what do you really use to download your files. If you do use another class/framework/..., you get an idea.
I would like to suggest you to use UIWebview for both case either it is PDF or image url from server.
[imgwebVw loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strImgURL]]];
If you really must do this you can read the file headers but I wouldn't suggest it. here is a category I wrote to get the size of a remote image, you can adapt it to detect if the file is indeed an image or not. And you can do similar stream parsing to search for the PDF Header: %PDF-1.0 etc. Otherwise you would assume it is a url.
In my application, I have a webview which loads up a page that contains both normal links and custom URL schemes, such as myapp://id=1234.
Right now, I am trying to trap the request within this function:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType{
NSLog(#"NSURLRequest = %#", [[request URL] absoluteString]);
}
Printing out the url as above gives me this:
http://localhost:8888/myapp://id=1234
Which is my localhost setup on MAMP with the custom URL appended to the URL.
I had hoped that I could use [request scheme] directly, but in this case now, it simply returns "http". Is there a way that I can handle custom URL schemes from within my own app?
At this point, I would like to be able to do the following:
User is on the webview
User taps the custom url link ( myapp://id=1234 )
My app handles the custom url and directs the user to another page ( http://www.someotherpage.com/?id=1234 )
Thank you!
When a URL has a double-slash following the colon, like yours does, the next part is supposed to look like userinfo#hostname:port (both userinfo# and :port can be omitted). Since id=1234 does not look like a hostname, the URL parser is not recognizing your URL as an absolute URL. So the URL parser treats it as a relative URL.
Try changing your URL syntax to either myapp:id=1234 or myapp:///id=1234.
I guess you shall declare any custom URL scheme as handled by your application.
Check reference
I'm looking for a solution for reading the http status code with a UIWebView.
I have found this page on the topic How do I get the last HTTP Status Code from a UIWebView? but i cannot use AsiHttpRequest in my case.
Si I was wondering if somebody have found a solution since 2009, and if something similar to NSLog(#"Status code = %#",[webView stringByEvaluatingJavaScriptFromString:#"document.status"]);
could possibly work.
Thanks,
I don't think you can get it from the UIWebView, but I think it would work to have the result of an HTTP request put into an NSString, then parse the status code out of the header part of that string, thing feed that string to a UIWebView.
See the NSURL Class Reference and the URL Loading Programming Guide.
A possible alternative would be to implement an HTTP proxy directly inside your App, then feed a localhost URL to UIWebView. Your proxy would just make an HTTP connection with the web server and sit passively by while UIWebView drives the HTTP protocol. You then snoop on the incoming data before passing it on to UIWebView from your proxy. That would avoid the need to buffer the whole page in an NSString before feeding it to your UIWebView.
I am looking to find out if a web page has changed, I was going to use the content length of the web page but have not seen a way to do so. Any ideas? Or can anyone think of another way to check periodically if a web page has changed?
If you mean with changed wether navigation has occured, you could use a custom UIWebViewDelegate and set a flag when e.g. -(void)webViewDidFinishLoad: occured.
You might want to check UIWebViews property request to check wether the URL actually differs.
If you want to check wether the content has changed you could retrieve it e.g. like this:
NSString* script = #"document.body.innerHTML";
NSString* content = [webView stringByEvaluatingJavaScriptFromString:script];
Or retrieve the length e.g. like this:
NSString* script = #"document.body.innerHTML.length";
int length = [[webView stringByEvaluatingJavaScriptFromString:script] integerValue];
I am assuming you want to know if the web page you are already showing is different than the web page you would get if you hit the server again.
You cannot do much with the documented interfaces in UIWebView.
You can use an NSURLConnection to ask for just the headers of a web page and not the actual content. Once you get the headers, look at fields like "Last-Modified" and "Content-Length" to see if it has changed. You can also look into the 304 not modified response code.
Set the HTTPMethod of a new NSURLRequest to HEAD instead of GET to not get the body.
Set your class as the delegate of an NSURLConnection created with that request.
Handle the following delegate callback and examine headers in the response.
-(void) connection:(NSURLConnection *)inConnection didReceiveResponse:(NSURLResponse *)inResponse;
For more information look here:
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
On the iphone, when trying to play a broken url using MPMoviePlayerController the user gets an alertbox with the message "the server is not correctly configured".
Is there any way to change this to something more user-friendly? Alternatively, is there any way to get an error status from the player instead of getting this message?
Thanks in advance..
MPMoviePlayer provides two notifications for the case of a broken / invalid movie URL:
From MPMoviePlayer initWithContentURL:
To check for errors in URL loading, register for the MPMoviePlayerContentPreloadDidFinishNotification or
MPMoviePlayerPlaybackDidFinishNotification notifications.
On error, these notifications contain an NSError
object available using the #"error" key in the notification’s userInfo dictionary.
You should be able to hook to one oh these notifications and perform the needed actions
if a broken URL is about to be loaded.
Usually this error message means that the web server that's serving the file does not support HTTP byte ranges.
iPhone OS uses HTTP byte ranges for streaming audio and video content. This makes it possible to "scrub" forwards and backwards in the content without downloading the entire content first.
Once I've got this error when trying to play urlencoded url string. I removed urlencoding method call before ask MPMoviePlayerController to play url and everything ok.