Widgets will let you present custom content in the messaging channel. Whenever a widget is added to your app build, the app will be able to parse and present widget messages, and will let users compose and send widget messages.
To create custom widget, you will need to create a framework project in xCode and follow the requirenemts.
Add "ChatKit" dependency to your framework using cocoapods Podfile
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/magnetsystems/Specs.git'
platform :ios, '9.0'
inhibit_all_warnings!
use_frameworks!
pod 'ChatKit',
# Add other dependencies here
Generate Workspace using pod install
shell command.
Controller
class, that implements ChatKit.WidgetControlling
protocol.Override widget name and model type
public override init() {
super.init()
name = "TextWidget"
modelType = Model.self
}
this name will be used to identify the widget type on the clinet and should be unique. Widget type will be used as a model.
Adding support for presentation types
public override init() {
super.init()
name = "TextWidget"
modelType = Model.self
registerPresenter(presenter: self,
forSurface: WidgetKit.WidgetSurface.Channel.rawValue)
registerPresenter(presenter: self, forSurface: WidgetKit.WidgetSurface.Creator.rawValue)
}
For Channel
presentation, the presenter should conform to ChannelWidgetPresenting
protocol.
For Creator
presentation, the presenter should conform to ChannelWidgetCreating
protocol.
Adding Channel support
Override following methods from ChannelWidgetPresenting
protocol, to addsupport for Channel surface.
func widgetViewPresentation() -> WidgetChannelPresentation
func widgetView() -> UIView
func configureWidgetView(widgetView:UIView, withMessage message:WidgetMessage, presentingNavigationController navigationController:UINavigationController?)
func widgetViewSizeForMessage( message:WidgetMessage ) -> CGSize
@objc optional func containerCellTypeForMessage( message:WidgetMessage ) -> WidgetContainerCell.Type
Adding Creator support
Override following methods from ChannelWidgetCreating
protocol, to add support for Creator surface.
func composerViewControllerWith(send: @escaping (WidgetModel) -> (), cancel: @escaping ()->()) -> UIViewController
var composerTitle:String {get}
To add your CustomWidget
widget to your app, you need to add the CustomWidget
framework and register your WidgetControllerBase
subclasss.
WidgetsController.sharedInstance.registerWidgetController(widgetController: CustomWidget.Controller())
Channel can be presented using ChannelViewController.
Channel view controller can be customized at some level.
Customize using a xib file
channelViewController.composerViewPresenter.setNib(nib:UINib)
Customize using a Class
channelViewController.composerViewPresenter.setClass(viewType:ChannelViewComposer)
channelViewController.channelTableViewController.channelCellProvider.cellType = CustomCellType.self
Create a view controller with a table view.
Create instance if ChannelTableViewController ans set tableView
property.
ChannelTableViewController.tableView = tableView
When creating a view controller with a table view. use instance of ChannelCellProvider to create cells, and to get their height.
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath,
forMessage message:WidgetMessage,
inNavigationController
navigationController:UINavigationController?) -> UITableViewCell
func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath,
forMessage message:WidgetMessage) -> CGFloat