Dan Hanly Dan Hanly - 10 days ago 6
Objective-C Question

Recognising UIWebView file types

I wish to detect when a PDF has been clicked and display it in a separate UIWebView. Here's what I have currently:

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {
NSURL *url = [request URL];
NSString *urlString = [url absoluteString];
if (fileType != @"PDF") {

if([urlString rangeOfString:@".pdf"].location == NSNotFound){
return true;
} else {
NSURL *filePath = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:filePath];
[pdfViewer loadRequest:requestObj];
[self.view addSubview:topView];

fileType = @"PDF";

return false;
}
} else {
return true;
}
}


This works fine. However it does have one Glaring Flaw:

What about "http://www.example.com/ikb3987basd"

How can I recognise a file type without the extension? Is there some sort of data about the file that I can check on?

Answer

You cannot know the content type of a response before you send the request to the server. At this moment, the client has no way of knowing what hides behind a certain URL. Only when the client receives the server's response can it inspect the Content-Type field in the HTTP header.

I believe what you want to achieve is not possible with the public UIWebView API (unless you first start an independent connection to retrieve the header of the URL and check the Content-Type of the response).

Comments