2011-04-25 92 views
2

我想获得在UIWebView中加载的文件的MIME类型,以便我可以将它们下载到我现在有的方法工作一些时间,有无论如何我可以做它更好?获取在UIWebView中加载的文件的MIME类型

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSURL *url = request.URL; 
    NSString *main = url.absoluteString; 
    //enter code here currentURL = main; 
    NSURLRequest *req = [NSURLRequest requestWithURL:url]; 
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self]; 
    [conn start]; 
    return YES; 
} 

NSString *mime = [response MIMEType]; 
NSLog(@"%@",mime); 
DownloadManagerAppDelegate *delegate = (DownloadManagerAppDelegate *)[[UIApplication sharedApplication] delegate]; 

DLMan *downloadView = [[DLMan alloc] init]; 
if ([mime rangeOfString:@"application/x-compressed"].length>0 || [mime rangeOfString:@"application/x-zip-compressed"].length>0 || [mime rangeOfString:@"application/zip"].length>0 || [mime rangeOfString:@"multipart/x-zip"].length>0) 
{ 
    self.tabBarController.selectedIndex = 1; 
    [[delegate myDownloadManager]addDownload:currentURL]; 
    NSLog(currentURL); 
    NSLog(@"download"); 
} 

回答

3

NSURLConnection委托didReceiveResponse使用这样的事情:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    NSString *mime = [response MIMEType]; 
} 

正如我已阅读,你没有得到正确的MIME您可能会感兴趣的另一种方法,创建plist的MIME类型例如mimeTypes.plist

(不一定是所有的,至少你要的类型与工作或处理类型)

使用它们加载到NSDicionary

NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath]; 
NSString *plistPath = [mainBundlePath stringByAppendingPathComponent:@"mimeTypes.plist"]; 

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath] 

通过搜索目标的扩展检查MIME:

[dict valueForKey:@"mp3"]; // will return audio/mpeg. 

请检查这个链接的列表MIME Types

+0

我试过你的第一个例子,你的第二个不会工作,因为我试图从filehosts下载,他们有时会在url中包含.zip或.mp3,然后才能下载它 – Heli0s 2011-04-26 05:21:53