Getting the mimetypes of files loaded in a UIWebView
I am trying to get the MIME Types of files loaded in a UIWebView so that I can download them the method i have right now works some of the times, is there anyway I can do it better?
- (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.tabBarContr开发者_JAVA技巧oller.selectedIndex = 1;
[[delegate myDownloadManager]addDownload:currentURL];
NSLog(currentURL);
NSLog(@"download");
}
In the NSURLConnection
delegate didReceiveResponse
use something like this:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSString *mime = [response MIMEType];
}
As I have read that you're not getting The MIME correctly you might be interested in another approach, create a plist
of the MIME Types for example mimeTypes.plist
(Not necessarily all the types at least the types you are going to work with or handle)
Load them into a NSDicionary
using:
NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
NSString *plistPath = [mainBundlePath stringByAppendingPathComponent:@"mimeTypes.plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath]
Check the MIME by searching the Target's Extension:
[dict valueForKey:@"mp3"]; // will return audio/mpeg.
Please check this link for the list of MIME Types.
精彩评论