2011-04-25 65 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