> ## Documentation Index
> Fetch the complete documentation index at: https://docs.castled.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Default App Inbox

> [!NOTE]
>
> **Prerequisites**
>
> * [SDK integration completed](https://docs.castled.io/developer-resources/sdk-integration/ios/initialization.md)
> * `enableAppInbox` config set to `true` during SDK initialization.

This is the quickest way to add **App Inbox** functionality to your iOS app. In this setup app will be using the default notification
center view controller provided by the SDK for displaying inbox messages and managing them. You can also customize the appearance of the notification center
view controller to match your app theme.

## Pod Install

Starting from Castled iOS version `4.0.2`, AppInbox functionality is separated as a new module `CastledInbox`. Add the module pod as dependency in the app pod file.

```bash
pod 'CastledInbox'
```

Save the **Podfile** and run the following command in the terminal to install the SDK:

```bash
pod install
```

## Displaying App Inbox content

To facilitate the effortless display of App Inbox content, the SDK provides a default view controller: `CastledInboxViewController`.
This controller is designed to showcase inbox cards and can be integrated within your app in two primary ways:

### Using Navigation Controller

Embed `CastledInboxViewController` within your existing navigation flow for a consistent user experience.

```swift swift
import CastledInbox

func pushViewController() {
    let inboxViewController = CastledInbox.sharedInstance.getInboxViewController(withUIConfigs: nil, andDelegate: self)
    self.navigationController?.pushViewController(inboxViewController, animated: true)
}
```

```objc objc
#import <CastledInbox-Swift.h>

- (void)pushViewController {
    UIViewController *inboxViewController = [[CastledInbox sharedInstance] getInboxViewControllerWithUIConfigs:nil andDelegate:self];
    [self.navigationController pushViewController:inboxViewController animated:YES];
}
```

### Using a Modal

Present the App Inbox cards in a modal view, suitable for focused interactions.

```swift swift
  import CastledInbox

  func presentViewController() {
      let inboxViewController = CastledInbox.sharedInstance.getInboxViewController(withUIConfigs: nil, andDelegate: self)
      self.present(inboxViewController, animated: true, completion: nil)
  }
```

```objc objc
  #import <CastledInbox-Swift.h>

  -(void)presentViewController {
      UIViewController *inboxViewController = [[CastledInbox sharedInstance] getInboxViewControllerWithUIConfigs:nil andDelegate:self];
      [self presentViewController:inboxViewController animated:YES completion:nil];
  }
```

## Handling Inbox Item Selection

To effectively capture and act upon interactions with App Inbox items, SDK offers the `CastledInboxViewControllerDelegate` protocol.
By adopting this protocol, you can tap into the callback provided when a user selects an item from the inbox.

```swift swift
@objc public protocol CastledInboxViewControllerDelegate {
    // Called when a Card is clicked by the user
    @objc optional func didSelectedInboxWith(_ buttonAction: CastledButtonAction, inboxItem: CastledInboxItem)

}
```

## Integration Steps:

1. Ensure your view controller conforms to the `CastledInboxViewControllerDelegate` protocol.
2. Implement the `didSelectedInboxWith((\_:buttonAction:, inboxItem:)` delegate method.

```swift swift
    class MyViewController: CastledInboxViewControllerDelegate {
    ...
        // Implement the callback method
        func didSelectedInboxWith(_ buttonAction: CastledButtonAction, inboxItem: CastledInboxItem) {
            print("didSelectedInboxWith title '\(buttonAction.buttonTitle ?? "")' uri '\(buttonAction.actionUri ?? "")'kvPairs \(buttonAction.keyVals) inboxItem\(inboxItem)")
            switch buttonAction.actionType {
                case .deepLink:
                    break
                case .navigateToScreen:
                    break
                case .richLanding:
                    break
                case .requestForPush:
                    break
                case .dismiss:
                    break
                case .custom:
                    break
                default:
                    break
            }
        }

    }
```

```objc objc
    @interface MyViewController () <CastledInboxViewControllerDelegate>

    @implementation ViewController

    - (void)didSelectedInboxWith:(CastledButtonAction *)buttonAction inboxItem:(CastledInboxItem *)inboxItem{

        NSLog(@"didSelectedInboxWith ----button title '%@' uri '%@' kvPairs  %@ item %@",buttonAction.buttonTitle,buttonAction.actionUri,buttonAction.keyVals,inboxItem);
        switch (buttonAction.actionType) {
            case CastledClickActionTypeDeepLink:

                break;
            case CastledClickActionTypeNavigateToScreen:

                break;
            case CastledClickActionTypeRichLanding:

                break;
            case CastledClickActionTypeRequestForPush:

                break;
            case CastledClickActionTypeDismiss:

                break;
            case CastledClickActionTypeCustom:

                break;
            default:
                break;
        }
    }


```

## Customising App Inbox Display

```swift swift
func showAppInbox() {
    // Customise the App Inbox styling using the below mentioned parameters
    let style = CastledInboxDisplayConfig()
    style.inboxViewBackgroundColor = .white
    style.navigationBarBackgroundColor = .link
    style.navigationBarTitle = "Castled Inbox"
    style.navigationBarButtonTintColor = .white
    style.loaderTintColor = .blue
    // style.hideBackButton = true
    // style.backButtonImage = UIImage(named: 'back_image')


    // Categories tab styling
    style.showCategoriesTab = true
    style.tabBarDefaultTextColor = .green
    style.tabBarSelectedTextColor = .brown
    style.tabBarDefaultBackgroundColor = .purple
    style.tabBarSelectedBackgroundColor = .lightGray
    style.tabBarIndicatorBackgroundColor = .red

    let inboxViewController = CastledInbox.sharedInstance.getInboxViewController(with: style, andDelegate: self)
    navigationController?.pushViewController(inboxViewController!, animated: true)
}

```

```objc objc
- (void)showAppInbox {
    // Customise the App Inbox styling using the below mentioned parameters
    CastledInboxDisplayConfig *style = [[CastledInboxDisplayConfig alloc] init];
    style.inboxViewBackgroundColor = [UIColor whiteColor];
    style.navigationBarBackgroundColor = [UIColor linkColor];
    style.navigationBarTitle = @"Castled Inbox";
    style.navigationBarButtonTintColor = [UIColor whiteColor];
    style.loaderTintColor = [UIColor blueColor];
    // style.hideBackButton = YES;
    // style.backButtonImage = [UIImage imageNamed:"back_image"];


    // Categories tab styling
    style.showCategoriesTab = YES;
    style.tabBarDefaultTextColor = [UIColor greenColor];
    style.tabBarSelectedTextColor = [UIColor brownColor];
    style.tabBarDefaultBackgroundColor = [UIColor purpleColor];
    style.tabBarSelectedBackgroundColor = [UIColor lightGrayColor];
    style.tabBarIndicatorBackgroundColor = [UIColor redColor];

    UIViewController *inboxViewController = [[CastledInbox sharedInstance] getInboxViewControllerWith:style andDelegate:self];
    [self.navigationController pushViewController:inboxViewController animated:YES];
}

```

## Retrieving Unread Message Count

The SDK offers multiple methods to obtain the count of unread messages, suitable for display purposes. Developers can choose from the following options based on their specific use-case:

### Approach 1 : Listener Approach

```swift swift
CastledInbox.sharedInstance.observeUnreadCountChanges(listener: { unreadCount in
    print("Inbox unread count is \(unreadCount)")
})

```

```objc objc
[[CastledInbox sharedInstance] observeUnreadCountChangesWithListener:^(NSInteger unreadCount) {
    NSLog(@"Inbox unread count is %ld", (long)unreadCount);
}];

```

### Approach 2 : Direct call

```swift swift
CastledInbox.sharedInstance.getInboxUnreadCount()
```

```objc objc
[[CastledInbox sharedInstance] getInboxUnreadCount];
```

## Dismiss App Inbox

Use the method below to dismiss the App Inbox programmatically.

```swift swift
CastledInbox.sharedInstance.dismissInboxViewController()
```

```objc objc
[[CastledInbox sharedInstance] dismissInboxViewController];
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.