zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS开发之网络编程--获取文件的MIMEType

2023-09-14 08:57:29 时间

前言:有时候我们需要获取文件的MIMEType的信息,下面就介绍关于获取MIMEType的方法。

 

1、直接百度搜索关键字"MIMEType",你会找到,然后查吧:

2、用代码获取文件的MIMEType信息:

#import "GetMIMEType.h"

#import MobileCoreServices/MobileCoreServices.h 

@implementation GetMIMEType

#pragma mark - 类方法

+ (NSString*)getMIMETypeURLRequestAtFilePath:(NSString*)path{

 return [[[GetMIMEType alloc] init] getMIMETypeURLRequestAtPath:path];

+ (NSString *)getMIMETypeWithCAPIAtFilePath:(NSString *)path{

 return [[[GetMIMEType alloc] init] getMIMETypeWithCAPIAtFilePath:path];

#pragma mark - 对象方法

//向该文件发送请求,根据请求头拿到该文件的MIMEType

-(NSString *)getMIMETypeURLRequestAtPath:(NSString*)path

 //1.确定请求路径

 NSURL *url = [NSURL fileURLWithPath:path];

 //2.创建可变的请求对象

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

 //3.发送请求

 NSHTTPURLResponse *response = nil;

 [NSURLConnection sendSynchronousRequest:request returningResponse: response error:nil];

 NSString *mimeType = response.MIMEType;

 return mimeType;

//调用C语言的API来获得文件的MIMEType ,只能获取本地文件哦,无法获取网络请求来的文件

-(NSString *)getMIMETypeWithCAPIAtFilePath:(NSString *)path

 if (![[[NSFileManager alloc] init] fileExistsAtPath:path]) {

 return nil;

 CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[path pathExtension], NULL);

 CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);

 CFRelease(UTI);

 if (!MIMEType) {

 return @"application/octet-stream";

 return (__bridge NSString *)(MIMEType)

@end

运行:

github源码下载:https://github.com/HeYang123456789/GetMIMEType


ios打包如何生成p12格式的证书和证书profile文件 做过H5多端开发ios app的同学们,肯定知道,打包ios应用,需要一个.p12后缀的ios打包证书和一个证书profile文件。 苹果官方提供的方法,生成这个证书需要苹果开发者账号和mac苹果电脑,但是我们大多数uniapp的开发者,都是使用windows电脑进行开发的,没有mac电脑,那么如何在windows电脑上生成ios证书呢?