我正在尝试将文件的 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/