Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 538
Dan Hanly Avatar asked Dec 09 '25 23:12

Dan Hanly


1 Answers

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).

like image 173
Ole Begemann Avatar answered Dec 11 '25 12:12

Ole Begemann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!