---
metadata:
  - name: generator
    content: Diplodoc Platform v5.66.0
alternate:
  - https://appmetrica.yandex.com.tr/docs/en/sdk/ios/analytics/quick-start.md
  - https://appmetrica.yandex.com.tr/docs/ru/sdk/ios/analytics/quick-start.md
  - https://appmetrica.yandex.com.tr/docs/zh/sdk/ios/analytics/quick-start.md
  - href: https://appmetrica.yandex.com.tr/docs/zh/sdk/ios/analytics/quick-start.md
    type: text/markdown
    title: Markdown version
  - href: https://appmetrica.yandex.com.tr/docs/zh/llms.txt?revision=r21975373
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://appmetrica.yandex.com.tr/docs/zh/llms.txt?revision=r21975373

# SDK 集成

iOS SDK 以包含源代码的模块形式提供。

## 步骤 1. 启用库 {#integration}

{% list tabs %}

- CocoaPods

   要连接分析库，请将以下依赖项添加到项目的 Podfile 中：

   ```ruby translate=no
   pod 'AppMetricaAnalytics', '~> 6.6.0'
   ```

   #### {#app-for-kids}
   {% cut "我有一个儿童应用" %} 

   如果您的应用针对儿童，请连接不使用`AppMetricaAdSupport`的模块：

   ```ruby translate=no
   pod 'AppMetricaCore', '~> 6.6.0'
   pod 'AppMetricaCrashes', '~> 6.6.0'
   pod 'AppMetricaWebKit', '~> 6.6.0'
   ```

   {% endcut %}

   {% cut "我不需要所有功能" %}

   如果您不需要 AppMetrica SDK 的所有功能，只连接必要的模块：

   ```ruby translate=no
   pod 'AppMetricaCore', '~> 6.6.0' # The main module for working with the AppMetrica SDK, mandatory for connection
   pod 'AppMetricaAdSupport', '~> 6.6.0' # Required for collecting IDs. IDFAs won't be collected without it
   pod 'AppMetricaCrashes', '~> 6.6.0' # The module for detecting crashes and sending errors
   pod 'AppMetricaWebKit', '~> 6.6.0' # The module for setting up event sending from the WebView's JS code
   ```

   {% endcut %}

- SPM 在 Xcode

   #### 通过Xcode界面进行集成

   要连接库，请执行以下步骤：

   1. 在 Xcode 项目导航器 (**项目导航器**) 窗口中，选择您的项目（如果使用 **工作区**）。 在顶部菜单中，点击**文件**，然后选择**添加包依赖...**。

      ![项目导航器](../../../../_images/xcode_add_package_dependency.png)

   2. 指定仓库 URL `https://github.com/appmetrica/appmetrica-sdk-ios`

      ![仓库 URL](../../../../_images/appmetrica-spm-url.png)

   3. 只保留需要的模块（要禁用模块，请在“添加到目标”中选择`无`）：``

      ![必需模块](../../../../_images/spm-select-packages.png)

      <!-- source: zh/sdk/_includes/ios-integration.md -->
      * `AppMetricaCore`：必需的主要 SDK 模块。 它必须连接才能使用 AppMetrica。
      * `AppMetricaAdSupport`：该模块用于收集ID，包括IDFA。
      * `AppMetrica崩溃`：该模块用于检测崩溃并发送错误。
      * `AppMetricaWebKit`：该模块允许您从WebView中的JavaScript代码发送事件。
      <!-- endsource: zh/sdk/_includes/ios-integration.md -->

      <!-- source: zh/sdk/_includes/ios-integration.md -->
      {% note info %}

      如果您的应用针对儿童，请连接不使用`AppMetricaAdSupport`的模块。

      {% endnote %}
      <!-- endsource: zh/sdk/_includes/ios-integration.md -->

- Package.swift

   #### 通过 Package.swift 清单进行集成

   首先，将以下依赖项添加到你的包的 `dependencies:` 数组中：

   ```swift translate=no
   dependencies: [
      .package(
         url: "https://github.com/appmetrica/appmetrica-sdk-ios",
         from: "6.6.0"
      )
   ],
   ```

   您可以根据项目需求连接的 AppMetrica SDK 模块：

   <!-- source: zh/sdk/_includes/ios-integration.md -->
   * `AppMetricaCore`：必需的主要 SDK 模块。 它必须连接才能使用 AppMetrica。
   * `AppMetricaAdSupport`：该模块用于收集ID，包括IDFA。
   * `AppMetrica崩溃`：该模块用于检测崩溃并发送错误。
   * `AppMetricaWebKit`：该模块允许您从WebView中的JavaScript代码发送事件。
   <!-- endsource: zh/sdk/_includes/ios-integration.md -->

   <!-- source: zh/sdk/_includes/ios-integration.md -->
   {% note info %}

   如果您的应用针对儿童，请连接不使用`AppMetricaAdSupport`的模块。

   {% endnote %}
   <!-- endsource: zh/sdk/_includes/ios-integration.md -->

   示例：在目标的 `dependencies` 数组中连接模块

   ```swift translate=no
   .target(
      name: "MyTargetName",
      dependencies: [
         .product(name: "AppMetricaCore", package: "AppMetrica"),
         .product(name: "AppMetricaCrashes", package: "AppMetrica"),
         .product(name: "AppMetricaWebKit", package: "AppMetrica"),
         // .product(name: "AppMetricaAdSupport", package: "AppMetrica"), // This module is disabled
      ]
   ),
   ```
- 使用 Package.swift 的 Tuist

   #### 通过 Tuist 与 Package.swift 清单集成
   
   将AppMetrica添加到你的Tuist/Package.swift
   
   ```
   import PackageDescription

   #if TUIST
       import struct ProjectDescription.PackageSettings

       let packageSettings = PackageSettings(
           productTypes: [:]
       )
   #endif

   let package = Package(
       name: "NewTuist",
       dependencies: [
           .package(url: "https://github.com/appmetrica/appmetrica-sdk-ios", from: "5.0.0")
       ]
   )
   ````
   
   现在在 Project.swift 中为 target 添加依赖。 
   AppMetrica 需要 "-ObjC" 链接器标志。 为所有与 AppMetrica 静态链接的目标添加设置，例如：
   
   ```
   依赖项: [
       .external(name: "AppMetricaCore"),
   ],
   设置: .settings(
      基础: ["OTHER_LDFLAGS": "$(inherited) -ObjC"]
   )
   ```

{% endlist %}

## 步骤 2. 初始化库 {#initialization}

{% list tabs group=instructions %}

- Swift

   添加导入：

   ```swift translate=no
   import AppMetricaCore
   ```

   在你的 `application(_:didFinishLaunchingWithOptions:)` 方法中初始化库：`UIApplicationDelegate`

   ```swift translate=no
   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
       // Initializing the AppMetrica SDK.
       let configuration = AppMetricaConfiguration(apiKey: "API key")
       AppMetrica.activate(with: configuration!)
   }
   ```

- Objective-C

   添加导入：

   ```obj-c translate=no
   #import <AppMetricaCore/AppMetricaCore.h>
   ```

   在您的 `application:didFinishLaunchingWithOptions:` 方法中初始化库：`UIApplicationDelegate`

   ```obj-c translate=no
   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   {
       // Initializing the AppMetrica SDK.
       AMAAppMetricaConfiguration *configuration = [[AMAAppMetricaConfiguration alloc] initWithAPIKey:@"API key"];
       [AMAAppMetrica activateWithConfiguration:configuration];
       return YES;
   }
   ```

{% endlist %}

{% cut "什么是 API 密钥？" %}

<!-- source: zh/sdk/_includes/api-key.md -->
API 密钥是在AppMetrica网页界面进行[应用注册](https://appmetrica.yandex.com/application/new)时分配的唯一应用 ID。 你可以在 **设置** → **主页** 中找到它。
<!-- endsource: zh/sdk/_includes/api-key.md -->

{% endcut %}

AppMetrica 支持跟踪预安装的应用。 要了解更多信息，请参见[跟踪预安装应用](https://appmetrica.yandex.com.tr/docs/zh/mobile-tracking/preinstalled-app-attr.md)。

## 步骤 3. (可选) 配置事件发送、用户属性和收入 {#send}

1. 要收集用户在应用中的操作信息，请设置发送自定义事件。 要了解更多信息，请参见[发送自定义事件](https://appmetrica.yandex.com.tr/docs/zh/data-collection/about-events.md)。

2. 要收集用户信息，请设置发送用户属性。 要了解更多信息，请查看[用户配置文件](https://appmetrica.yandex.com.tr/docs/zh/data-collection/about-profiles.md)。

   {% note info %}

   与事件不同，个人资料属性只能有一个值。 当您发送新的属性值时，旧的值会被覆盖。

   {% endnote %}

4. 要跟踪应用内购买，请设置收入发送。 要了解更多信息，请查看[应用内购买](https://appmetrica.yandex.com.tr/docs/zh/data-collection/about-revenue.md)。

## 第 4 步。 测试库的操作 {#test}

要测试库的功能：

1. 启动包含 AppMetrica SDK 的应用，并使用一段时间。
2. 确保您的设备已连接到互联网。
3. 在 AppMetrica 界面中，请确保：
   * 在 [受众](https://appmetrica.yandex.com.tr/docs/zh/mobile-reports/audience-report.md) 报告中有新用户。
   * 在 **Engagement** → **Sessions** 报告中的会话数已增加。
   * 在 [Events](https://appmetrica.yandex.com.tr/docs/zh/mobile-reports/events-report.md) 和 [Profiles](https://appmetrica.yandex.com.tr/docs/zh/mobile-reports/profile-report.md) 报告中有事件和个人资料属性.

## 了解更多 {#learn-more}

- [发送自定义事件](https://appmetrica.yandex.com.tr/docs/zh/sdk/ios/analytics/ios-operations.md#report-event)
- [发送个人资料属性](https://appmetrica.yandex.com.tr/docs/zh/sdk/ios/analytics/ios-operations.md#send-attribute-profile)
- [发送电商事件](https://appmetrica.yandex.com.tr/docs/zh/sdk/ios/analytics/ios-operations.md#send-ecommerce)
- [发送收入数据](https://appmetrica.yandex.com.tr/docs/zh/sdk/ios/analytics/ios-operations.md#send-revenue)
- [发送广告收入数据](https://appmetrica.yandex.com.tr/docs/zh/sdk/ios/analytics/ios-operations.md#send-adrevenue)
- [如何启用发送用户位置？](https://appmetrica.yandex.com.tr/docs/zh/troubleshooting/troubleshooting.md#region)

## 故障排查 {#faq}

- [会话数量没有增加](https://appmetrica.yandex.com.tr/docs/zh/sdk/ios/analytics/ios-errors.md#faq-session-count)
- [报告中没有事件](https://appmetrica.yandex.com.tr/docs/zh/sdk/ios/analytics/ios-errors.md#no-events)
- [Swift 包管理器出现问题](https://appmetrica.yandex.com.tr/docs/zh/sdk/ios/analytics/ios-errors.md#swift-manager)
- [我的问题没有列出](https://appmetrica.yandex.com.tr/docs/zh/sdk/ios/analytics/ios-errors.md#not-found)

<!-- source: zh/_includes/feedback-button-3.md -->
如果您没有找到所需答案，可以通过反馈表单提交您的问题。 请尽可能详细地描述问题。 如果可能，请附上截图。

<a href="../troubleshooting/feedback-new">
  <span class="button">联系支持</span>
</a>

<a href="../troubleshooting/feedback-docs">
  <span class="button">为文档提出改进建议</span>
</a>
<!-- endsource: zh/_includes/feedback-button-3.md -->
