> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/unity-plugin/building-for-supported-platforms/building-for-ios-ipados.md).

# 为 iOS/iPadOS 构建

## 先决条件

在开始之前，请确保你已准备好以下内容：

* Unity 2022.3 或更高版本
* Xcode（建议使用最新版本）
* Apple Developer 账户
* 已集成并正常运行 Convai Unity SDK 的项目
* 用于构建并部署到 iOS/iPadOS 的 MacBook

### 步骤 1：准备你的 Unity 项目

1. 打开你的启用了 Convai 的 Unity 项目。
2. 确保你拥有最新版本的 Convai Unity SDK [已导入](/api-docs/zh/cha-jian-yu-ji-cheng/unity-plugin/setting-up-unity-plugin.md)[ 并设置](/api-docs/zh/cha-jian-yu-ji-cheng/unity-plugin/setting-up-unity-plugin.md) 到你的项目中。

<figure><img src="/files/65b4dea71ceb79e0369cc7b398ed6b767b38e5e4" alt=""><figcaption><p>已导入 Convai SDK 的 Unity 项目</p></figcaption></figure>

### 步骤 2：配置构建设置

1. 在 Unity 中，前往 `文件` → `构建设置`.
2. 选择 `iOS` 作为目标平台。
3. 点击 `切换平台` 如果尚未选择。
4. 勾选 `开发构建` 用于测试的选项。

<figure><img src="/files/d1553a7627a98af35b2fae10ffedcd987af811b8" alt=""><figcaption><p>Unity 构建设置窗口，已选择 iOS 并勾选 Development Build</p></figcaption></figure>

{% hint style="info" %}
如果你想手动添加几个必需文件，请按照步骤 3 操作。如果你希望自动完成，请跳到步骤 4
{% endhint %}

### 步骤 3：手动添加必需文件

#### 添加 link.xml

1. 创建一个名为 `link.xml` 的新文件，位于项目的 `Assets` 文件夹中。
2. 将以下内容添加到该文件中：

```xml
<linker>
  <assembly fullname="UnityEngine">
    <type fullname="UnityEngine.Application" preserve="fields">
      <property name="platform"/>
    </type>
  </assembly>
</linker>
```

<figure><img src="/files/b4c79d03f3befbf5e483930a4efeaf15aa1aded5" alt=""><figcaption><p>Unity 项目视图，显示 Assets 文件夹中的 link.xml 文件</p></figcaption></figure>

此文件可防止潜在的 `FileNotFoundException` 错误，相关于 `libgrpc_csharp_ext.x64.dylib` 文件。

#### 添加 BuildIos.cs 脚本

1. 在 `Assets/Convai/Scripts` 中创建一个新的 C# 脚本， `命名为`.
2. iOSBuild.cs

```csharp
将以下内容添加到该脚本：
#if UNITY_EDITOR && UNITY_IOS
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

public class iOSBuild : MonoBehaviour
{
    [PostProcessBuild]
    public static void OnPostProcessBuild(BuildTarget target, string path)
    {
        string projectPath = PBXProject.GetPBXProjectPath(path);
        PBXProject project = new PBXProject();
        project.ReadFromString(File.ReadAllText(projectPath));
#if UNITY_2019_3_OR_NEWER
        string targetGuid = project.GetUnityFrameworkTargetGuid();
#else
        string targetGuid = project.TargetGuidByName(PBXProject.GetUnityTargetName());
#endif
        project.AddFrameworkToProject(targetGuid, "libz.tbd", false);
        project.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
        File.WriteAllText(projectPath, project.WriteToString());
    }
}
#endif
```

### 步骤 4：为 iOS 安装所需的 gRPC DLL：

1. 前往 Convai -> Custom Package Installer
2. 点击 `安装 iOS 构建包`
3. 将脚本附加 `命名为` 到场景中的任意 GameObject 上。

<figure><img src="/files/36bc614464651c928a7278ae095d8b6b0c2e795b" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/0f70b14700bb08aea589a658f32a2b4a1d18624a" alt=""><figcaption></figcaption></figure>

### 步骤 5：构建 Xcode 项目

1. 在 Unity 中，前往 `文件` → `构建设置`.
2. 点击 `构建` 并选择一个位置来保存你的 Xcode 项目。
3. 等待 Unity 生成 Xcode 项目。

### 步骤 6：在 Xcode 中配置并构建

1. 打开生成的 Xcode 项目。
2. 在 Xcode 中，在导航器里选择你的项目。
3. 在 "TARGETS" 部分下选择你的目标。
4. 切换到 "Signing & Capabilities" 选项卡。
5. 确保已勾选 "Automatically manage signing"。
6. 从下拉菜单中选择你的 Team（这需要 Apple Developer 账户）。
7. 如有需要，将 Bundle Identifier 更改为唯一字符串。

<figure><img src="/files/1ac7852c5a11d06e944ffc656f4eefb170443fc6" alt=""><figcaption><p>Xcode 窗口显示 Signing &#x26; Capabilities 选项卡，Team 和 Bundle Identifier 字段被高亮</p></figcaption></figure>

### 步骤 7：构建并运行

1. 将你的 iOS 设备连接到 Mac。
2. 在 Xcode 中，将已连接的设备选为构建目标。
3. 点击 "Play" 按钮或按 `Cmd + R` 即可在设备上构建并运行应用。

<figure><img src="/files/c998433c48eb838f1e9efa92aa35bf0dad90c244" alt=""><figcaption><p>Xcode 工具栏显示已选择已连接设备，并高亮 "Play" 按钮</p></figcaption></figure>

### 故障排除

* 如果你遇到任何构建错误，请确保以上所有步骤都已正确完成。
* 检查你的 Apple Developer 账户是否具备必要的描述文件和证书。
* 如果你遇到任何与 GRPC 相关的问题，请验证 `libgrpc_csharp_ext.a` 和 `libgrpc.a` 文件是否正确放置在 `Assets/Convai/Plugins/gRPC/Grpc.Core/runtime/ios` 文件夹中。

<figure><img src="/files/0f70b14700bb08aea589a658f32a2b4a1d18624a" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/unity-plugin/building-for-supported-platforms/building-for-ios-ipados.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
