0

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!

Nuzhat Zari
  • 3,398
  • 2
  • 24
  • 36

3 Answers3

0

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.

zrzka
  • 20,249
  • 5
  • 47
  • 73
0

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]]];
iOS Test
  • 1,103
  • 1
  • 11
  • 18
  • UIWebView s*cks ... Slow, bloated, ... :) – zrzka Jul 16 '12 at 11:51
  • Use uiwebview Delegate methods...UiwebviewDidStartLoad/UiwebviewDidfinishLoad to show a uiactivity indicator while content is loading from server url. – iOS Test Jul 16 '12 at 11:57
  • You mean to say I can use uiwebview to load image, pdf and url?I do not need to find out whether response is image, pdf or url?[imgwebVw loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strImgURL]]]; will work for all three? – Nuzhat Zari Jul 16 '12 at 12:07
  • if you are getting a server url for all cases either pdf url or image url or other content url, it will works for all on uiwebview method [imgwebVw loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strImgURL]]]; just pass the url in the method. – iOS Test Jul 16 '12 at 12:13
0

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.

Community
  • 1
  • 1
Paul de Lange
  • 10,613
  • 10
  • 41
  • 56