开发者

Determine MIME Type of NSData Loaded From a File

开发者 https://www.devze.com 2023-03-05 14:13 出处:网络
For various reasons I am intercepting http requests and loading content from files in my app\'s document directory using NSURLProtocol. Part of the proces开发者_开发百科s involves loading an NSData ob

For various reasons I am intercepting http requests and loading content from files in my app's document directory using NSURLProtocol. Part of the proces开发者_开发百科s involves loading an NSData object, which could be anything from an html file to a jpeg image. NSURLProtocol requires setting a mimetype.

Is there an API in the iPhone-SDK to determine the mimetype of the file or NSData content?


Perhaps you could download the file and use this to get the file's MIME type.

+ (NSString*) mimeTypeForFileAtPath: (NSString *) path {
    if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        return nil;
    }
    // Borrowed from https://stackoverflow.com/questions/5996797/determine-mime-type-of-nsdata-loaded-from-a-file
    // itself, derived from  https://stackoverflow.com/questions/2439020/wheres-the-iphone-mime-type-database
    CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[path pathExtension], NULL);
    CFStringRef mimeType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
    CFRelease(UTI);
    if (!mimeType) {
        return @"application/octet-stream";
    }
    return [NSMakeCollectable((NSString *)mimeType) autorelease];
}


can you not see anything in the HTTP accept headers, if you are intercepting requests i would expect the accept header to be set to the mime of the expected type in most cases

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号