识别UIWebView文件类型

我希望检测何时单击PDF并将其显示在单独的UIWebView中。这是我目前的情况:
- (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;
    }   
}
这很好用。然而,它确实有一个耀眼的缺陷: 那么“http://www.example.com/ikb3987basd”呢 如何在没有扩展名的情况下识别文件类型?是否有关于我可以检查的文件的某种数据?     
已邀请:
在将请求发送到服务器之前,您无法知道响应的内容类型。此时,客户端无法知道隐藏在某个URL后面的内容。只有当客户端收到服务器的响应时,才能检查HTTP头中的Content-Type字段。 我相信使用public
UIWebView
API是不可能实现的(除非您首先启动独立连接以检索URL的标头并检查响应的Content-Type)。     
使用狭义相对论,你可以证明当你甚至没有它时就不可能知道一个文件....:p 而且,不是分析'pdf'的整个URL,我只是看看你可以得到的文件扩展名 [[url absoluteString] pathExtension]     
您可以使用
NSURLProtocol
sublcass来捕获由
UIWebView
(但不是(!)
WKWebView
)生成的所有请求和响应。 在
AppDelegate
inѭѭѭ:
[NSURLProtocol registerClass:[MyCustomProtocol class]];
这将迫使
MyCustomProtocol
处理所有网络请求。 在执行
MyCustomProtocol
之类的(代码未测试):
+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    return YES;
}

+ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request{
    return request;
}

- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id<NSURLProtocolClient>)client
{
    self = [super initWithRequest:request cachedResponse:cachedResponse client:client];
    if (self) {
         return self;
    }
    return nil;
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
    // parse response here for mime-type and content-disposition
    if (shouldDownload) {
        // handle downloading response.URL
        completionHandler(NSURLSessionResponseBecomeDownload);
    } else {
        completionHandler(NSURLSessionResponseAllow);
    }
    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];

}
有关NSURLProtocol的更多详细信息,您可以在Apple的示例和此处找到     

要回复问题请先登录注册