xcodensurlconnectionmime-typesfile-extension

Convert mimetype to file extension


how to convert mimetype to file extension in xcode for example i have this code

File Extension     MIME Type

aifc                audio/x-aiff
avi                 video/x-msvideo
gz                  application/x-gzip
jpg                 image/jpeg

Xcode

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
        NSString *mime      = [response MIMEType];
        NSString *extension = @"" // how to covert it ?!
}

Solution

  • include <MobileCoreServices/MobileCoreServices.h> or <CoreServices/CoreServices.h> then just following code:

    CFStringRef mimeType = (CFStringRef)@"audio/x-aiff";
    CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType, NULL);
    CFStringRef extension = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassFilenameExtension);