Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 识别UIWebView文件类型_Objective C_Pdf_Uiwebview_Uiwebviewdelegate - Fatal编程技术网

Objective c 识别UIWebView文件类型

Objective c 识别UIWebView文件类型,objective-c,pdf,uiwebview,uiwebviewdelegate,Objective C,Pdf,Uiwebview,Uiwebviewdelegate,我希望检测PDF何时被单击,并将其显示在单独的UIWebView中。以下是我目前拥有的: - (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; { NSURL *url = [request URL]; NSString *urlString =

我希望检测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头中的内容类型字段


我相信使用public
UIWebView
API无法实现您想要的目标(除非您首先启动一个独立的连接来检索URL的标题并检查响应的内容类型)使用狭义相对论,你可以证明,当你连一个文件都没有的时候,你是不可能知道它的

此外,我不会分析“pdf”的整个URL,我只会查看您可以从中获得的文件扩展名


[[url absoluteString]pathExtension]

您可以使用
NSURLProtocol
子类捕获由
UIWebView
生成的所有请求和响应(但不是(!)
WKWebView

中的AppDelegate
中,
使用选项完成启动:

[NSURLProtocol registerClass:[MyCustomProtocol class]];
这将强制
MyCustomProtocol
处理所有网络请求

MyCustomProtocol
的实现中,类似(未测试的代码):

+(BOOL)canInitWithRequest:(NSURLRequest*)request{
返回YES;
}
+(NSURLRequest*)规范请求请求请求:(NSURLRequest*)请求{
返回请求;
}
-(id)initWithRequest:(NSURLRequest*)请求缓存响应:(NSCachedURLResponse*)缓存响应客户端:(id)客户端
{
self=[super initWithRequest:request cachedResponse:cachedResponse client:client];
如果(自我){
回归自我;
}
返回零;
}
-(void)URLSession:(NSURLSession*)会话数据任务:(NSURLSessionDataTask*)数据任务didReceiverResponse:(NSURLResponse*)响应完成处理程序:(void(^)(NSURLSessionResponseDisposition))完成处理程序
{
//在此处分析mime类型和内容处置的响应
如果(应下载){
//处理下载response.URL
completionHandler(NSURLSessionResponseBecomeDownload);
}否则{
completionHandler(NSURLSessionResponseAllow);
}
[self.client URLProtocol:self-didReceiverResponse:response-cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}

您可以在苹果的和

中找到更多关于您的详细信息,您强调“公共UIWebView API”的方式暗示您知道一种方式,但不使用该API?这是否意味着您考虑了一个解决方案,但需要下载另一个组件?不,我不知道怎么办。但是很明显,
UIWebView
只是一个非常稀疏的API,而不是一个更复杂的框架(WebKit),这是一个遗憾。苹果显然可以公开比他们更多的
UIWebView
的工作原理(与
WebView
及其在OS X上的众多代理相比)。我已经提到了我能想到的唯一解决方案。独立检索相关URL的标题。在发送请求后,如何知道响应内容类型?
+ (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];

}