Skip to main content

How to send custom JSON Data in a post?

This guide explains the steps required to implement a custom widget in LikeMinds Feed SDK for iOS. Follow the instructions below to enable, send, and render a custom widget in posts.

Sending Custom Widget in a Post

Attaching custom data to posts enables developers to enhance the functionality and interactivity of their feed application. The LikeMinds Feed SDK provides the flexibility to attach metadata ([String: Any]) to posts, allowing you to define custom widgets or features tailored to your application's specific requirements.

By overriding the post creation logic in LMFeedCreatePostScreen and utilizing the createPost() method in LMFeedCreatePostViewModel, you can seamlessly pass custom data with each post.

Steps to attach Custom Widget in a Post

Step 1: Create a Custom View Controller

To enable sending a custom widget, create a custom view controller that extends LMFeedCreatePostScreen. This custom view controller will include your logic for configuring and sending the custom widget.

CustomCreatePostScreen.swift
import LikeMindsFeedCore

class CustomCreatePostScreen: LMFeedCreatePostScreen {
/// Function to create a post with a custom widget
/// Pass a metadata dictionary (`[String: Any]`) to attach custom widget data.
/// This metadata will be used to create a custom widget and will be attached
/// to the post model in the response.
@objc func createPostWithCustomWidget(metadata: [String: Any]) {
viewModel?.createPost(
content: nil,
heading: nil,
topics: nil,
files: nil,
linkPreview: nil,
poll: nil,
meta: metadata
)
}

/// Trigger the function to create a post with a custom widget
func onTapCreatePost() {
createPostWithCustomWidget(metadata: ["key": "Custom Widget Data"])
}
}

Step 2: Replace the Default LMFeedCreatePostScreen in Components Class:

In AppDelegate.swift file, after calling the setupFeed() method, Use Components to replace the default implementation with your custom view controller for LMFeedCreatePostScreen.

AppDelegate.swift
import LikeMindsChatUI

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// YOUR_CODE
LMChatCore.shared.setupChat(deviceId: deviceId)

Components.shared.createPostScreen = CustomCreatePostScreen.self
return true
}

Render Custom Widget in a Post

Rendering custom views for posts allows you to create personalized and dynamic feed experiences tailored to your application's needs. By extending the LikeMinds Feed SDK, you can use metadata to attach additional data to posts and render custom UI elements based on this data. This approach enables developers to go beyond default post templates.

In this guide, we will walk you through rendering custom views for posts containing metadata. By extending the LMFeedPostCustomCell class, you can define custom layouts and behavior to dynamically render data specific to each post.

Steps to Render Custom Widget in a Post

Step 1: Create a Custom Cell

To render a custom widget in posts, create a custom cell that extends LMFeedPostCustomCell. This cell will define how the custom widget is displayed in the feed.

LMChatCustomCell.swift
import LikeMindsChatUI
import LikeMindsChatCore

class CustomCell: LMChatCustomCell {
// Create a custom view to display in the CustomCell

open override func setupViews() {
super.setupViews()

contentView.addSubview(containerView)
containerView.addSubview(contentStack)

contentStack.addArrangedSubview("Your Views")
}

override func configure(for indexPath: IndexPath, with data: LMFeedPostContentModel) {
super.configure(for: indexPath, with: data)
// Use the data provided to access the metadata
}
}

Step 2: Replace the Default LMFeedPostCustomCell

In the AppDelegate.swift file, replace the default LMFeedPostCustomCell with your custom implementation. Use the LMUIComponents registry to register your custom view controller.

AppDelegate.swift
import LikeMindsChatUI

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// YOUR_CODE
LMFeedCore.shared.setupFeed(deviceId: deviceId)
// This will replace the default LMFeedPostCustomCell with your custom implementation
LMUIComponents.shared.customCell = CustomClientView.self
return true
}