-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathURL+Mime.swift
34 lines (29 loc) · 1.06 KB
/
URL+Mime.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// URL+Mime.swift
// Pods
//
// Created by BAO HA on 23/10/24.
//
import Foundation
import MobileCoreServices
import UniformTypeIdentifiers
extension URL {
func getMimeType() -> String {
let pathExtension = self.pathExtension.lowercased()
if #available(iOS 14.0, *) {
// Sử dụng UniformTypeIdentifiers (UTType) cho iOS 14+
if let utType = UTType(filenameExtension: pathExtension) {
return utType.preferredMIMEType ?? "application/octet-stream"
}
} else {
// Sử dụng MobileCoreServices (kUTType) cho iOS 13 trở xuống
if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as CFString, nil)?.takeRetainedValue(),
let mimeType = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue()
{
return mimeType as String
}
}
// Trả về MIME type mặc định nếu không tìm thấy
return "application/octet-stream"
}
}