Skip to content
Tauri 中文网

macOS 应用包

应用包是在 macOS 上执行的包格式。它是一个简单的目录,包含你的应用成功运行所需的一切,包括你的应用可执行文件、资源、Info.plist 文件和其他文件,如 macOS 框架。

¥An application bundle is the package format that is executed on macOS. It is a simple directory that includes everything your application requires for successful operation, including your app executable, resources, the Info.plist file and other files such as macOS frameworks.

要将你的应用打包为 macOS 应用包,你可以使用 Tauri CLI 并在 Mac 计算机中运行 tauri build 命令:

¥To package your app as a macOS application bundle you can use the Tauri CLI and run the tauri build command in a Mac computer:

npm run tauri build -- --bundles app

文件结构

¥File structure

macOS 应用包是一个具有以下结构的目录:

¥The macOS app bundle is a directory with the following structure:

├── <productName>.app
│ ├── Contents
│ │ ├── Info.plist
│ │ ├── ...additional files from [`tauri.conf.json > bundle > macOS > files`]
│ ├── MacOS
│ │ ├── <app-name> (app executable)
│ ├── Resources
│ │ ├── icon.icns (app icon)
│ │ ├── ...resources from [`tauri.conf.json > bundle > resources`]
│ ├── _CodeSignature (codesign information generated by Apple)
│ ├── Frameworks
│ ├── PlugIns
│ ├── SharedSupport

有关更多信息,请参阅 官方文档

¥See the official documentation for more information.

原生配置

¥Native configuration

应用包由 Info.plist 文件配置,该文件包含具有应用标识的键值对以及 macOS 读取的配置值。

¥The app bundle is configured by the Info.plist file, which includes key-value pairs with your app identity and configuration values read by macOS.

Tauri 会自动配置最重要的属性,例如应用二进制名称、版本。打包包标识符、最低系统版本等。

¥Tauri automatically configures the most important properties such as your app binary name, version. bundle identifier, minimum system version and more.

要扩展配置文件,请在 src-tauri 文件夹中创建一个 Info.plist 文件并包含你想要的密钥对:

¥To extend the configuration file, create an Info.plist file in the src-tauri folder and include the key-pairs you desire:

src-tauri/Info.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSCameraUsageDescription</key>
<string>Request camera access for WebRTC</string>
<key>NSMicrophoneUsageDescription</key>
<string>Request microphone access for WebRTC</string>
</dict>
</plist>

Info.plist 文件与 Tauri CLI 生成的值合并。覆盖默认值(例如应用版本)时要小心,因为它们可能会与其他配置值冲突并引入意外行为。

¥This Info.plist file is merged with the values generated by the Tauri CLI. Be careful when overwriting default values such as application version as they might conflict with other configuration values and introduce unexpected behavior.

有关更多信息,请参阅 官方 Info.plist 文档

¥See the official Info.plist documentation for more information.

Info.plist 本地化

¥Info.plist localization

Info.plist 文件本身仅支持一种语言,通常是英语。如果你想支持多种语言,可以为每种附加语言创建 InfoPlist.strings 文件。每个文件都属于应用包中 Resources 目录中其自己语言特定的 lproj 目录。

¥The Info.plist file by itself only supports a single language, typically English. If you want to support multiple languages, you can create InfoPlist.strings files for each additional language. Each file belongs in its own language specific lproj directory in the Resources directory in the application bundle.

要自动打包这些文件,你可以利用 Tauri 的 resources 功能。为此,请按照以下模式在项目中创建文件结构:

¥To bundle these files automatically you can leverage Tauri’s resources feature. To do that, create a file structure in your project following this pattern:

├── src-tauri
│ ├── tauri.conf.json
│ ├── infoplist
│ │ ├── de.lproj
│ │ │ ├── InfoPlist.strings
│ │ ├── fr.lproj
│ │ │ ├── InfoPlist.strings

虽然 infoplist 目录名称可以自由选择,只要你在下面的资源配置中更新它,lproj 目录必须遵循 <lang-code>.lproj 命名,字符串目录文件必须命名为 InfoPlist.strings(大写 i 和 p)。在大多数情况下,语言代码应该是 BCP 47 后面的两个字母代码。

¥While the infoplist directory name can be chosen freely, as long as you update it in the resources config below, the lproj directories must follow the <lang-code>.lproj naming and the string catalogue files must be named InfoPlist.strings (capital i and p). For most cases the language code should be a two letter code following BCP 47.

对于上面显示的 Info.plist 示例,de.lproj > InfoPlist.strings 文件可能如下所示:

¥For the Info.plist example shown above, the de.lproj > InfoPlist.strings file could look like this:

de.lproj/InfoPlist.strings
NSCameraUsageDescription = "Kamera Zugriff wird benötigt für WebRTC Funktionalität";
NSMicrophoneUsageDescription = "Mikrofon Zugriff wird benötigt für WebRTC Funktionalität";

最后,使用上面提到的资源功能让 Tauri 拾取这些文件:

¥Lastly, make Tauri pick up these files by using the resources feature mentioned above:

src-tauri/tauri.conf.json
{
"bundle": {
"resources": {
"infoplist/**": "./"
}
}
}

权利

¥Entitlements

授权是一种特殊的 Apple 配置键值对,充当授予你的应用特定功能的权利或特权,例如充当用户的默认电子邮件客户端和使用 App Sandbox 功能。

¥An entitlement is a special Apple configuration key-value pair that acts as a right or privilege that grants your app particular capabilities, such as act as the user’s default email client and using the App Sandbox feature.

当你的应用签名时,将应用权利。有关更多信息,请参阅 代码签名文档

¥Entitlements are applied when your application is signed. See the code signing documentation for more information.

要定义应用所需的权利,你必须创建权利文件并配置 Tauri 以使用它。

¥To define the entitlements required by your application, you must create the entitlements file and configure Tauri to use it.

  1. src-tauri 文件夹中创建一个 Entitlements.plist 文件并配置你的应用所需的键值对:

    ¥Create a Entitlements.plist file in the src-tauri folder and configure the key-value pairs you app requires:

src-tauri/Entitlements.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
</dict>
</plist>
  1. 配置 Tauri 以使用 Entitlements.plist 文件:

    ¥Configure Tauri to use the Entitlements.plist file:

tauri.conf.json
{
"bundle": {
"macOS": {
"entitlements": "./Entitlements.plist"
}
}
}

有关更多信息,请参阅 官方文档

¥See the official documentation for more information.

最低系统版本

¥Minimum system version

默认情况下,你的 Tauri 应用支持 macOS 10.13 及更高版本。如果你使用的 API 需要较新的 macOS 系统,并且希望在你的应用包中强制执行该要求,则可以配置 tauri.conf.json > bundle > macOS > minimumSystemVersion 值:

¥By default your Tauri application supports macOS 10.13 and above. If you are using an API that requires a newer macOS system and want to enforce that requirement in your app bundle, you can configure the tauri.conf.json > bundle > macOS > minimumSystemVersion value:

tauri.conf.json
{
"bundle": {
"macOS": {
"minimumSystemVersion": "12.0"
}
}
}

包括 macOS 框架

¥Including macOS frameworks

如果你的应用需要其他 macOS 框架才能运行,你可以在 tauri.conf.json > bundle > macOS > frameworks 配置中列出它们。框架列表可以包括系统或自定义框架和 dylib 文件。

¥If your application requires additional macOS frameworks to run, you can list them in the tauri.conf.json > bundle > macOS > frameworks configuration. The frameworks list can include either system or custom frameworks and dylib files.

tauri.conf.json
{
"bundle": {
"macOS": {
"frameworks": [
"CoreAudio",
"./libs/libmsodbcsql.18.dylib",
"./frameworks/MyApp.framework"
]
}
}
}

:::note 注意

  • 要引用系统框架,你只需使用其名称(不带 .framework 扩展名)即可,而不必使用绝对路径

    ¥To reference a system framework you can just use its name (without the .framework extension) instead of absolute path

  • 系统框架必须存在于 $HOME/Library/Frameworks/Library/Frameworks//Network/Library/Frameworks/

    ¥System frameworks must exist in either the $HOME/Library/Frameworks, /Library/Frameworks/, or /Network/Library/Frameworks/

  • 要引用本地框架和 dylib 文件,你必须使用相对于 src-tauri 目录的完整框架路径

    ¥To reference local frameworks and dylib files you must use the complete path to the framework, relative to the src-tauri directory

:::

添加自定义文件

¥Adding custom files

你可以使用 tauri.conf.json > bundle > macOS > files 配置将自定义文件添加到应用包中,该包将目标路径映射到相对于 tauri.conf.json 文件的源。文件被添加到 <product-name>.app/Contents 文件夹中。

¥You can use the tauri.conf.json > bundle > macOS > files configuration to add custom files to your application bundle, which maps the destination path to its source relative to the tauri.conf.json file. The files are added to the <product-name>.app/Contents folder.

tauri.conf.json
{
"bundle": {
"macOS": {
"files": {
"embedded.provisionprofile": "./profile-name.provisionprofile",
"SharedSupport/docs.md": "./docs/index.md"
}
}
}
}

在上面的例子中,profile-name.provisionprofile 文件被复制到 <product-name>.app/Contents/embedded.provisionprofiledocs/index.md 文件被复制到 <product-name>.app/Contents/SharedSupport/docs.md

¥In the above example, the profile-name.provisionprofile file is copied to <product-name>.app/Contents/embedded.provisionprofile and the docs/index.md file is copied to <product-name>.app/Contents/SharedSupport/docs.md.


Tauri 中文网 - 粤ICP备13048890号