Skip to content

Instantly share code, notes, and snippets.

@Judas
Last active March 20, 2023 13:51
Show Gist options
  • Save Judas/cb68e51e18538734ad6347562c4599ce to your computer and use it in GitHub Desktop.
Save Judas/cb68e51e18538734ad6347562c4599ce to your computer and use it in GitHub Desktop.
iAdvize iOS SDK - Custom Chat Button
class CustomerClass {
func updateChatButton() {
guard IAdvizeSDK.shared.activationStatus == .activated else {
hideChatButton()
return
}
guard !IAdvizeSDK.shared.chatboxController.isChatboxPresented() else {
hideChatButton()
return
}
guard IAdvizeSDK.shared.targetingController.ongoingConversation() != nil ||
IAdvizeSDK.shared.targetingController.isActiveTargetingRuleAvailable else {
hideChatButton()
return
}
print("Showing Chat button.")
// Keep a track that the button was displayed to display it again when the chatbox
// will be dismissed.
displayChatButton = true
// Be sure that the Chat Button isn't already displayed or,
// if it's the case, be sure that we update his superview.
// Otherwise, do nothing.
guard chatButtonView.superview == nil else {
chatButtonView.superview?.bringSubviewToFront(chatButtonView)
return
}
guard let view = UIWindow.key else {
print("Oops. It seems that we can't find any view to display the chat button. Please specify a view or a window when calling `showChatButton()`")
return
}
chatButtonView.removeFromSuperview()
view.addSubview(chatButtonView)
chatButtonView.installConstraints()
}
func iAdvizeLogout() {
IAdvizeSDK.shared.logout()
updateChatButton()
}
func hideChatButton() {
print("Hiding Chat button.")
// Keep a track that the button was hidden to not display it when the chatbox
// will be dismissed.
displayChatButton = false
// When the button is set to be explicitly hidden we remove it from its superview because
// it can be added to another view when shown later.
chatButtonView.removeFromSuperview()
}
}
extension CustomerClass: TargetingControllerDelegate {
func activeTargetingRuleAvailabilityDidUpdate(isActiveTargetingRuleAvailable: Bool) {
// SDK active rule availability changed to isActiveTargetingRuleAvailable
updateChatButton()
}
}
extension CustomerClass: ConversationControllerDelegate {
func ongoingConversationUpdated(ongoingConversation: IAdvizeConversationSDK.OngoingConversation?) {
// SDK ongoing conversation status changed
updateChatButton()
}
func didReceiveNewMessage(content: String) {
// A new message was received via the SDK
}
func conversationController(_ controller: ConversationController, shouldOpen url: URL) -> Bool {
// A message link was tapped, return true if you want your app to handle it
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment