This library allows you to determine the MIME type or UTType for any data and is a Swift wrapper for libmagic(3) which is part of file(1) command.
- Sources for libmagic: https://github.com/apple-opensource/file
- Home page for file: https://www.darwinsys.com/file/
The C sources of libmagic are not part of this repository. A
Makefile downloads, compiles and bundles them into the
libmagic.xcframework and compiles the
magic.mgc definitions file into Sources/Magic/Resources.
The Makefile should be executed only if you wish to use another version of
libmagic. To do so, update the version number in
libmagic.version and run make:
makeAlternatively, you can also pass the version to make using the VERSION variable:
make VERSION=5.48you can include Magic in your Xcode project via File | Swift Packages | Add Package Dependencies.... In the first step of the wizard use the URL https://github.com/macmoonshine/magic, press Next and following the remaining steps of the wizard.
dependencies: [
.package(url: "https://github.com/macmoonshine/magic.git"),
],
targets: [
.target(
name: "MyTarget",
dependencies: [.product(name: "Magic", package: "Magic")]
),
]You can use the class Magic to determine the media type of files or data as a
string.
import Magic
let magic = Magic(definition: .default)
let data: Data! = "{}".data(using: .utf8)
let path = "..."
print(magic.file(data: data))
print(magic.file(path: path))The definition: default directive tells libmagic to use the default location
for the magic.mgc file. But you can also use your own file via:
let magic = Magic(definition: .path("/.../magic.mgc"))Further possibilities can be found in Magic.Definition. Without a definition,
the initialisation of Magic will fail.
Magic also offers an extension for Apple's Uniform Type Identifiers:
import Magic
import UniformTypeIdentifiers
let data: Data! = "{}".data(using: .utf8)
let dataType = UTType(data: data)
let fileTye = UTType(path: "...")Furthermore, Magic provides the struct MediaType, which was implemented on the
basis of RFC 2045 Section 5.1
and that allows easy access to the properties of media types. This allows, for
example, a simple selection of views in SwiftUI depending on the data content.
import Magic
import SwiftUI
struct Preview: View {
@State var data: Data
var mediaType: MediaType? {
return MediaType(data: data)
}
var body: some View {
if let mediaType = mediaType {
switch mediaType.mainType {
case .audio:
AudioPlayer(data: data)
case .image:
ImagePreview(data: data)
case .text:
TextPreview(data: data, encoding: mediaType.charset)
default:
Text("No preview available.")
}
}
else {
Text("Cannot determine media type.")
}
}
}The MediaType structure also makes it easy to create syntactically correct media type strings:
import Magic
let mediaType = MediaType(
type: .application, subtype: "soap+xml",
parameters: .charset(.utf8), .init(name: "action", value: "urn:CreateCredential")
)
let headers = ["Content-Type": mediaType.description]Magic.Definition.system — the definition file of the system's file command
— was removed in version 1.1. Use .builtin instead, which uses the magic
definitions bundled with this package.
Copyright 2025 macoonshine
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
The bundled libmagic (Frameworks/libmagic.xcframework and
Sources/Magic/Resources/magic.mgc) is derived from file(1) and subject to
its own copyright and license:
Copyright (c) Ian F. Darwin 1986, 1987, 1989, 1990, 1991, 1992, 1994, 1995. Software written by Ian F. Darwin and others; maintained 1994- Christos Zoulas.
This software is not subject to any export provision of the United States Department of Commerce, and may be exported to any country or planet.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice immediately at the beginning of the file, without modification, this list of conditions, and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.