我正在尝试在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");
}
答案 0 :(得分: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。
列表