iOS開發之網路編程--擷取檔案的MIMEType,iosmimetype
前言:有時候我們需要擷取檔案的MIMEType的資訊,下面就介紹關於擷取MIMEType的方法。
1、直接百度搜尋索引鍵"MIMEType",你會找到,然後查吧:
2、用代碼擷取檔案的MIMEType資訊:
1 #import "GetMIMEType.h" 2 3 #import <MobileCoreServices/MobileCoreServices.h> 4 5 @implementation GetMIMEType 6 7 #pragma mark - 類方法 8 + (NSString*)getMIMETypeURLRequestAtFilePath:(NSString*)path{ 9 return [[[GetMIMEType alloc] init] getMIMETypeURLRequestAtPath:path];10 }11 12 + (NSString *)getMIMETypeWithCAPIAtFilePath:(NSString *)path{13 return [[[GetMIMEType alloc] init] getMIMETypeWithCAPIAtFilePath:path];14 }15 #pragma mark - 對象方法16 //向該檔案發送請求,根據要求標頭拿到該檔案的MIMEType17 -(NSString *)getMIMETypeURLRequestAtPath:(NSString*)path18 {19 //1.確定請求路徑20 NSURL *url = [NSURL fileURLWithPath:path];21 22 //2.建立可變的請求對象23 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];24 25 //3.發送請求26 NSHTTPURLResponse *response = nil;27 [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];28 29 NSString *mimeType = response.MIMEType;30 return mimeType;31 }32 33 //調用C語言的API來獲得檔案的MIMEType ,只能擷取本地檔案哦,無法擷取網路請求來的檔案34 -(NSString *)getMIMETypeWithCAPIAtFilePath:(NSString *)path35 {36 if (![[[NSFileManager alloc] init] fileExistsAtPath:path]) {37 return nil;38 }39 40 CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[path pathExtension], NULL);41 CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);42 CFRelease(UTI);43 if (!MIMEType) {44 return @"application/octet-stream";45 }46 return (__bridge NSString *)(MIMEType)47 ;48 }49 50 @end
運行:
github源碼下載:https://github.com/HeYang123456789/GetMIMEType