Add a Pre-Conversation ViewController
Add and configure the Conversation in your iOS app.
When implemented, a user sees the following displayed below an article in the article view controller:
- Preview of comments (
preConversationVC
) from the conversation - Text box to enter comments
- Button to show all comments
If a user clicks the button to see more comments, the SDK opens a new ViewController which displays all comments from the conversation.
If a user clicks on the text box, the creation screen appears to enable the user to type a comment.
Requirement
Implementation
Use the following steps to add a Conversation:
- Implement the
SpotImLoginDelegate
protocol. This delegate is called when a user that is not logged in tries to post a comment when guest commenting is not allowed. An empty implementation can also be provided.
You can learn more about the
SpotImLoginDelegate
in the Implement SSO for the OpenWeb iOS SDK article.
extension ArticleViewController: SpotImLoginDelegate {
func startLoginUIFlow(navigationController: UINavigationController)
// Show your login UI flow here, you can either `push` or `present` it
// Once the flow is done and SSO is completed we will dismiss the whole navigation stack
// You can still dismiss your UI at the end, but we will do it regardless
}
func renewSSOAuthentication(userId: String) {
// Renew SSO - Basically silent SSO again to keep the user connected
// This function will be called once a token is no longer valid and a user was connected from before
// Follow the above steps to renew the SSO
}
}
- Create an instance of
SpotImSDKFlowCoordinator
. Make sure to have a strong reference, otherwise the instance ofSpotImSDKFlowCoordinator
will be deallocated.
SpotIm.createSpotImFlowCoordinator(loginDelegate: self) { [weak self] result in
guard let self = self else { return }
switch result {
case .success(let coordinator):
self.spotIMCoordinator = coordinator
case .failure(let error):
print(error)
}
}
- Instantiate
preConversationVC
for a specific article. Be sure to replace the placeholder text for thePOST_ID
,url
,title
,subtitle
, andthumbnailUrl
.
ThepreConversationVC
will be passed into thecompletion
block.
let articleMetadata = SpotImArticleMetadata(url: "URL TO THE ARTICLE PAGE ON THE WEB",
title: "ARTICLE TITLE",
subtitle: "ARTICLE SUBTITLE",
thumbnailUrl: "URL TO ARTICLE THUMBNAIL IMAGE")
spotIMCoordinator?.preConversationController(
withPostId: "POST ID",
articleMetadata: articleMetadata,
numberOfPreLoadedMessages: 2, // This is optional, Default = 2, Maximum = 15
navigationController: navigationController,
completion: { [weak self] preConversationVC in
// add preConversationVC to your view controller
}
)
- Add
preConversationVC
as a child to another view controller. - Add the
preConversationVC.view
to a view of another view controller or of a container view. - Lay out the
preConversationVC
view. - Call the
open func didMove(toParent parent: UIViewController?)
method of thepreConversationVC
instance.
preConversationVC.view.translatesAutoresizingMaskIntoConstraints = false
self.addChild(preConversationVC)
self.containerView.addSubview(preConversationVC.view)
preConversationVC.view.topAnchor.constraint(equalTo: self.containerView.topAnchor).isActive = true
preConversationVC.view.leadingAnchor.constraint(equalTo: self.containerView.leadingAnchor).isActive = true
preConversationVC.view.bottomAnchor.constraint(equalTo: self.containerView.bottomAnchor).isActive = true
preConversationVC.view.trailingAnchor.constraint(equalTo: self.containerView.trailingAnchor).isActive = true
preConversationVC.didMove(toParent: self)
Make sure the container view can be resized when the
PreConversationViewController
is filled with comments.
To understand when the view gets resized, implement theSpotImLayoutDelegate
protocol and get a callback through theviewHeightDidChange(to newValue: CGFloat)
method. To get the callback, make sure to set the layout delegate in theSpotImSDKFlowCoordinator
before trying to load the view.spotIMCoordinator.setLayoutDelegate(delegate: YOUR_DELEGATE_IMPLEMENTATION)
- (Optional) Customize the Conversation.
Updated 5 months ago