# ตั้งค่า InboxKit ด้วย CocoaPods

*ใช้งานได้ตั้งแต่ iOS SDK [7.0.40](https://github.com/Pushwoosh/pushwoosh-ios-sdk/releases/tag/7.0.40)*

InboxKit มาในรูปแบบ subspec ที่เป็นตัวเลือกของ pod หลัก `PushwooshXCFramework` คุณจำเป็นต้องผสานรวม SDK หลักไว้แล้ว หากคุณเริ่มต้นจากศูนย์ ให้ทำตาม [คู่มือการผสานรวมพื้นฐาน](/th/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 หลักแล้ว

## แสดง inbox

เพิ่ม inbox controller ไปยัง navigation flow ใดก็ได้ การกำหนดค่าเริ่มต้นก็เพียงพอที่จะทำให้ inbox ทำงานได้ด้วย cell มาตรฐานสามประเภท:

<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>

## ปรับแต่ง inbox

ใน Swift ให้กำหนดค่า controller ผ่าน value type `PushwooshInboxKitAttributes` ส่วนใน Objective-C ให้ใช้ setters ที่รองรับ `@objc` บน controller — เนื่องจาก `PushwooshInboxKitAttributes` เป็น Swift struct และไม่ได้ถูก bridged

<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 จะเปิดเผยสี, ฟอนต์, corner radii และ date formatter ทั้งหมดที่ใช้โดย cell เริ่มต้น ทุกค่าเป็นสี semantic ของ Apple โดยค่าเริ่มต้น ดังนั้น inbox จึงตอบสนองต่อ 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;">Cell ที่มีคำบรรยายพร้อมธีมที่กำหนดเองซึ่งปรับใช้ผ่าน <code>PushwooshInboxKitAttributes.Style</code></p>

## จัดการการแตะและการรีเฟรช

ทำตาม `PushwooshInboxKitDelegate` เพื่อตอบสนองต่อการกระทำของผู้ใช้และ event การรีเฟรช ทุกเมธอดมีการใช้งานเริ่มต้น ดังนั้นคุณจึงสามารถ override เฉพาะส่วนที่ต้องการได้:

<Tabs syncKey="code-example">
<TabItem label="Swift">
```swift
final class InboxCoordinator: NSObject, PushwooshInboxKitDelegate {
    func inboxKit(_ vc: PushwooshInboxKitViewController,
                  didSelect message: PWInboxMessageProtocol) -> Bool {
        // คืนค่า true เพื่อให้ SDK เปิด URL ของข้อความหรือ richmedia
        // คืนค่า false หากคุณจัดการการแตะทั้งหมดแล้ว (เช่น นำทางไปยังหน้าจอที่กำหนดเอง)
        return true
    }

    func inboxKit(_ vc: PushwooshInboxKitViewController,
                  didRefreshWith messages: [PWInboxMessageProtocol],
                  error: Error?) {
        // แสดงสถานะว่างเปล่า / ข้อผิดพลาดของคุณเองที่นี่หากจำเป็น
    }
}

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

SDK มี bulk operations เป็นเมธอด `@objc` บน controller ดังนั้นคุณจึงสามารถเชื่อมต่อเข้ากับ `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="การคงอยู่ของข้อมูล">
การดำเนินการ Mark-as-read, mark-all-as-read, delete และ clear-read ทั้งหมดจะถูกบันทึกอย่างถาวรลงในที่จัดเก็บ inbox ของ Pushwoosh ในเครื่องก่อนที่จะส่งคำขอไปยังเครือข่าย สถานะจะยังคงอยู่แม้จะรีสตาร์ทโปรเซส แม้ว่า backend จะยังไม่รับทราบการเปลี่ยนแปลงก็ตาม
</Aside>

## ขั้นตอนถัดไป

<LinkCard
  title="เอกสารอ้างอิง API ของ PushwooshInboxKit"
  description="เอกสาร DocC ที่สร้างขึ้นสำหรับ public type ทุกประเภท"
  href="https://pushwoosh.github.io/pushwoosh-ios-sdk/PushwooshInboxKit/documentation/pushwooshinboxkit/"
/>