# 公證你自己簽署的 Mac App

> 附加你用 Developer ID 簽署的 Mac App，讓 Agent 把它傳送到 Apple 的公證服務，在 Apple 拒絕時閱讀日誌，並把票證裝訂到檔案上。

Source: https://air.build/tw/docs/build/notarize-a-mac-app-you-signed/

如果你自己發佈 Mac App，例如在你的網站上提供下載，Apple 的公證服務會檢查它並傳回一張票證。Agent 可以把你簽署的 App 傳送到公證服務，告訴你 Apple 的答覆，並把票證裝訂到檔案上，讓票證隨 App 一起發佈。Apple 在 [Notarizing macOS software before distribution](https://developer.apple.com/documentation/security/notarizing-macos-software-before-distribution) 中說明了公證檢查什麼以及何時需要公證。

> [!NOTE]
> AirBuild 不會用 Developer ID 為 Mac App 簽署，也不會建立 Developer ID 憑證。在 AirBuild 中，`asc certificates create` 只會建立 Wallet 票證和 Apple Pay 憑證，而 AirBuild 自己簽署的建置版本用於 TestFlight 和 App Store。請先用你的 Developer ID 憑證自行為 App 簽署：請參閱 Apple 的 [Developer ID 憑證](https://developer.apple.com/help/account/certificates/create-developer-id-certificates/)。
>
> AirBuild 沒有用於公證的畫面。你在專案的聊天中詢問 Agent，它會使用 `asc notarization`（請參閱 [asc：簽署、裝置與使用者](https://air.build/tw/docs/cli/asc-signing/#notarization)中的 notarization）。

> [!NOTE]
> Apple 的 [Developer ID 憑證](https://developer.apple.com/help/account/certificates/create-developer-id-certificates/)頁面說明，建立 Developer ID 憑證需要你在 Apple 開發者團隊中擁有帳號持有人角色。

## 開始之前

- 團隊的擁有者已連結 Apple 開發者帳號並啟用一個團隊。Agent 會用該團隊把 App 傳送給 Apple。請參閱[連結你的 Apple 開發者帳號](https://air.build/tw/docs/app-store/connect-apple-developer-account/)。
- 你的 App 已用你的 Developer ID 簽署。上面的 Apple 頁面列出了公證服務對 App 的其他要求。
- 檔案為 `.zip`、`.dmg` 或 `.pkg`：這是該指令接受的三種類型。如果你附加的是 `.app`，AirBuild 會把它壓縮成 zip 後附加（例如 `MyApp.app.zip`）。如果 Apple 說簽署無效，請依照 Apple 的 [Customizing the notarization workflow](https://developer.apple.com/documentation/security/customizing-the-notarization-workflow#Export-a-package-for-notarization) 所示自行壓縮 App，或製作一個磁碟映像檔，然後附加它。

## 傳送給 Apple

1. 在專案的聊天中把檔案附加到一則訊息裡。請參閱[傳送照片、檔案和草圖給 Agent](https://air.build/tw/docs/projects/send-photos-and-files/)。

2. 詢問，例如：

   ```text
   對附件中的 Mac App 進行公證，附加票證，然後把附加了票證的檔案傳給我。
   ```

3. Agent 用 `asc notarization submit --wait` 傳送它。說明指出，該指令會把檔案上傳到 Apple，並在加上 `--wait` 時等待 Apple 的結果。如果它先停止了等待，請參閱下面關於逾時的問題。

4. 它會告訴你 Apple 的答覆和該提交的 ID。狀態為 `Accepted`、`In Progress`、`Invalid` 或 `Rejected` 之一。

傳送檔案不會變更它。當狀態為 `Accepted` 時，Agent 會繼續[裝訂票證](https://air.build/tw/docs/build/notarize-a-mac-app-you-signed/#staple-the-ticket)。

## 如果 Apple 不接受

當狀態為 `Invalid` 或 `Rejected` 時，指令會顯示「Notarization failed」，並指向日誌。詢問：

```text
Apple 為什麼沒有通過？給我看公證日誌裡寫了什麼。
```

Agent 用 `asc notarization log --fetch` 讀取日誌。說明指出，日誌包含「detailed information about the notarization result, including any issues found during the scan.」Agent 可以在聊天中摘要它，或把它儲存為檔案（`--path`）並傳給你。

App 是你簽署的，所以需要由你修正：依日誌的要求重新簽署，附加新檔案後再次詢問。Apple 在 [Resolving common notarization issues](https://developer.apple.com/documentation/security/resolving-common-notarization-issues) 中列出了常見問題及其修正方法。

## 裝訂票證

裝訂會把 Apple 的票證附加到你的檔案上。說明指出，`asc notarization staple` 會附加票證並隨即檢查。它會變更檔案本身，因此只有加上 `--confirm` 才會執行，Agent 會先詢問你。

- 可以裝訂的內容：已公證的 App 套件（`.app`）、磁碟映像檔（`.dmg`）或已簽署的安裝器套件（`.pkg`）。`.zip` 無法裝訂：說明指出要裝訂其中的項目，然後重新壓縮。Agent 會為你完成這些操作。
- 執行位置：說明指出裝訂只能在 Mac 上執行，而且 Apple 的裝訂工具可能需要連上網際網路。在本機團隊中，Agent 在你的 Mac 上工作，因此 Agent 會在那裡裝訂。在雲端團隊中，本頁無法確定 Agent 能否裝訂。如果它說裝訂工具「is supported on macOS only」，請在你的 Mac 上自行裝訂檔案：Apple 的 [Customizing the notarization workflow](https://developer.apple.com/documentation/security/customizing-the-notarization-workflow) 說明了方法。請參閱[選擇 Agent 的執行位置](https://air.build/tw/docs/get-started/where-agents-run/)。

Agent 會在聊天中傳回裝訂後的檔案。按住它（在 Mac 上按右鍵）並選擇 **另存為…**。在 Mac 上，按一下聊天中的 `.pkg` 或 `.dmg` 會在 Finder 中顯示它，而不是打開它。請參閱[傳送照片、檔案和草圖給 Agent](https://air.build/tw/docs/projects/send-photos-and-files/#open-files-in-the-chat)。

之後若要檢查某個檔案，請詢問「附加的 App 有裝訂的票證嗎？」Agent 會用 `asc notarization validate` 檢查，該指令不會變更檔案，同樣只能在 Mac 上執行。

## 之後查看提交

| 詢問 | `asc` |
| --- | --- |
| 「我的公證進度如何？」 | `notarization status`，提供該提交的 ID。 |
| 「列出我最近的五次公證。」 | `notarization list`，加上 `--limit`。 |
| 「給我看最近一次公證的日誌。」 | `notarization log`，提供該提交的 ID。不加 `--fetch` 時，它會提供日誌的連結，而不是日誌內容。 |

## 常見問題

**Agent 可以用 Developer ID 為我的 Mac App 簽署嗎？**

不可以。AirBuild 保存著你團隊的發佈憑證和開發憑證，並用它們為 TestFlight 和 App Store 建置版本簽署，但不會用 Developer ID 簽署。如果要求建立 Developer ID 憑證，`asc certificates create` 會說該類型「is a signing certificate, which Airbuild manages」。請自行為 App 簽署，然後附加它。

**Agent 說不支援該檔案類型。**

訊息為「unsupported file type … (must be .zip, .dmg, or .pkg)」。請壓縮 App，或把它放進磁碟映像檔或安裝器套件，然後附加。

**Agent 說檔案不能為空。**

收到的檔案中沒有任何內容。請在新訊息中重新附加。

**Agent 等待公證逾時。**

訊息以「timed out waiting for notarization」開頭。檔案已經上傳，Apple 會繼續檢查。稍後再詢問「我的公證進度如何？」。

**Agent 說 zip 封存檔無法裝訂。**

訊息為「zip archives cannot be stapled or validated directly; staple the contained item and recreate the archive」。請讓 Agent 裝訂 zip 中的 App，然後重新壓縮。

**Agent 說裝訂工具僅支援 macOS。**

裝訂只能在 Mac 上執行。請在你的 Mac 上自行裝訂檔案（請參閱 Apple 的 [Customizing the notarization workflow](https://developer.apple.com/documentation/security/customizing-the-notarization-workflow)），或使用本機團隊，其 Agent 在你的 Mac 上工作。

**Agent 說 xcrun 無法使用。**

訊息接著說：「install Xcode and ensure the active developer directory is configured」。在本機團隊中，請在 Mac 上安裝 Xcode，並在 **Xcode ▸ Settings ▸ Locations ▸ Command Line Tools** 中選擇它。請參閱[選擇 Agent 的執行位置](https://air.build/tw/docs/get-started/where-agents-run/#set-up-your-mac-for-a-local-team)。

**我的 Developer ID 憑證還能繼續使用嗎？**

詢問「我的 Developer ID 憑證中有受 2027 年 2 月到期影響的嗎？」Agent 會用 `asc certificates developer-id-check` 檢查。其說明指出，來自原 Developer ID 憑證授權單位的憑證將於 2027 年 2 月 1 日停止運作，用它們簽署的 `.pkg` 安裝器將無法再安裝，應替換為來自 Developer ID 憑證授權單位 (G2) 的憑證。每個憑證的結果為 `affected`、`not-affected`、`likely-affected` 或 `unknown`。
