有时候我们需要获取文件的MIMEType的信息,比如向服务器上传文件资源时需要将文件资源类型传给服务器,这样就可以判断文件是图片还是音视频,下面就介绍关于获取MIMEType的方法:
- (NSString *)fileMIMETypeURLSessionWithPath:(NSString*)path {
//1.确定请求路径
NSURL *url = [NSURL fileURLWithPath:path];
//2.创建可变的请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block NSString *mimeType = nil;
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
mimeType = response.MIMEType;
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return mimeType;
}
//向该文件发送请求,根据请求头拿到该文件的MIMEType
- (NSString *)fileMIMETypeURLConnectionWithPat