# 使用 CocoaPods 设置 InboxKit

*自 iOS SDK [7.0.40](https://github.com/Pushwoosh/pushwoosh-ios-sdk/releases/tag/7.0.40) 起可用。*

InboxKit 作为 `PushwooshXCFramework` pod 的一个可选子规范 (subspec) 提供。您需要先集成主 SDK；如果您是从头开始，请先遵循 [基本集成指南](/zh/developer/pushwoosh-sdk/ios-sdk/setting-up-pushwoosh-ios-sdk-7-0/basic-integration-guide/)。

## 添加 InboxKit pod

1. 打开您的 `Podfile` 并将 InboxKit subspec 添加到您的应用 target 中：

```ruby
target 'MyApp' do
  use_frameworks!

  pod 'PushwooshXCFramework'
  pod 'PushwooshXCFramework/PushwooshInboxKit'
end
```

2. 从您的项目目录运行 `pod install`：

```bash
pod install
```

3. 打开生成的 `.xcworkspace` 文件。现在 InboxKit 已与主 SDK 一起链接。

## 显示收件箱

将收件箱控制器添加到任何导航流中。默认配置足以让您获得一个包含三种标准单元格类型的工作收件箱：

<Tabs syncKey="code-example">
<TabItem label="Swift">
```swift
import PushwooshInboxKit

let inboxVC = PushwooshInboxKitViewController()
navigationController?.pushViewController(inboxVC, animated: true)
```
</TabItem>

<TabItem label="Objective-C">
```objective-c
@import PushwooshInboxKit;

PushwooshInboxKitViewController *inboxVC = [PushwooshInboxKitViewController new];
[self.navigationController pushViewController:inboxVC animated:YES];
```
</TabItem>
</Tabs>

## 自定义收件箱

在 Swift 中，通过 `PushwooshInboxKitAttributes` 值类型配置控制器。在 Objective-C 中，使用控制器上对 `@objc` 友好的设置器 — `PushwooshInboxKitAttributes` 是一个 Swift 结构体 (struct)，并且没有桥接。

<Tabs syncKey="code-example">
<TabItem label="Swift">
```swift
var attributes = PushwooshInboxKitAttributes()
attributes.pullToRefreshEnabled = true
attributes.swipeToDeleteEnabled = true
attributes.pinningEnabled = true
attributes.style.unreadBadgeColor = .systemBlue
attributes.style.titleFont = .systemFont(ofSize: 17, weight: .semibold)

let inboxVC = PushwooshInboxKitViewController(attributes: attributes)
```
</TabItem>

<TabItem label="Objective-C">
```objective-c
PushwooshInboxKitViewController *inboxVC = [PushwooshInboxKitViewController new];
[inboxVC setBackgroundColor:[UIColor systemBackgroundColor]];
[inboxVC setEmptyMessage:@"You have no messages yet"];
```
</TabItem>
</Tabs>

`Style` 结构体 (struct) 公开了默认单元格使用的所有颜色、字体、圆角半径和日期格式化器。默认情况下，每个值都是 Apple 的语义颜色，因此收件箱会自动响应系统的暗黑模式 (dark mode)。

<img src="/setting-up-pushwoosh-inboxkit-ios-custom.webp" alt="自定义样式的 InboxKit 信息流，品牌颜色应用于标题和未读指示器" width="280" style="display: block; margin: 0 auto;"/>

<p style="text-align: center; opacity: 0.7; font-size: 0.875rem; margin-top: 0.5rem;">带有通过 <code>PushwooshInboxKitAttributes.Style</code> 应用的自定义主题的带标题单元格。</p>

## 处理点击和刷新

遵循 `PushwooshInboxKitDelegate` 以响应用户操作和刷新事件。每个方法都有一个默认实现，因此您只需覆盖所需的方法：

<Tabs syncKey="code-example">
<TabItem label="Swift">
```swift
final class InboxCoordinator: NSObject, PushwooshInboxKitDelegate {
    func inboxKit(_ vc: PushwooshInboxKitViewController,
                  didSelect message: PWInboxMessageProtocol) -> Bool {
        // 返回 true 以让 SDK 打开消息 URL 或富媒体。
        // 如果您完全处理了点击（例如，路由到自定义屏幕），则返回 false。
        return true
    }

    func inboxKit(_ vc: PushwooshInboxKitViewController,
                  didRefreshWith messages: [PWInboxMessageProtocol],
                  error: Error?) {
        // 如果需要，在此处显示您自己的空/错误状态。
    }
}

inboxVC.delegate = inboxCoordinator
```
</TabItem>
</Tabs>

SDK 将批量操作作为控制器上的 `@objc` 方法提供，因此您可以将它们直接连接到 `UIBarButtonItem`：

<Tabs syncKey="code-example">
<TabItem label="Swift">
```swift
let markAll = UIBarButtonItem(
    image: UIImage(systemName: "checkmark.circle"),
    style: .plain,
    target: inboxVC,
    action: #selector(PushwooshInboxKitViewController.markAllAsRead)
)
let clearRead = UIBarButtonItem(
    image: UIImage(systemName: "trash"),
    style: .plain,
    target: inboxVC,
    action: #selector(PushwooshInboxKitViewController.clearReadMessages)
)
inboxVC.navigationItem.rightBarButtonItems = [clearRead, markAll]
```
</TabItem>
</Tabs>

<Aside type="note" title="持久性">
在网络请求发送之前，标记为已读、全部标记为已读、删除和清除已读都会持久化到本地 Pushwoosh 收件箱存储中。即使后端尚未确认更改，状态在进程重启后仍然存在。
</Aside>

## 后续步骤

<LinkCard
  title="PushwooshInboxKit API 参考"
  description="为每个公共类型生成的 DocC 文档。"
  href="https://pushwoosh.github.io/pushwoosh-ios-sdk/PushwooshInboxKit/documentation/pushwooshinboxkit/"
/>