iphone - 获取 UIWebView 中加载的文件的 mimetypes

标签 iphone ios4 uiwebview download mime-types

我正在尝试将文件的 MIME 类型加载到 UIWebView 中,以便我可以下载它们,我现在的方法有时可以工作,无论如何我可以做得更好吗?

- (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");
}

最佳答案

NSURLConnection 委托(delegate) didReceiveResponse 中使用如下内容:

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

据我所知,您没有正确获取 MIME,您可能对另一种方法感兴趣,创建 MIME 类型的 plist,例如 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 的列表.

关于iphone - 获取 UIWebView 中加载的文件的 mimetypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5775065/

相关文章:

iphone - 如何检查 iPhone 上使用的缓存磁盘空间量

ios - DiscoverServices 有时从不触发

xcode - 不使用 XCode 4 我会变得过时吗

html - 要隐藏的 Swift 和 UIWebView 元素

iphone - 如何防止iPhone应用程序在非视网膜设备上运行?

iphone - 更新应用程序会删除文件吗?

ios - CC_MD5() 和 CC_SHA1() 在 iOS 4 中可用吗?

ios - 如何从 UIWebView 获取 URL 的哈希片段

ios - uiwebview 正在缩小到 uiscrollview 内

iphone - UITextField 在 UITableView 中滚动时清除 - 自定义单元格