Authentication
Setup
There is a setup call that you need to do before registration or login. You can get your APP_ID by signing up on Kommunicate dashboard.
In your AppDelegate
, first import Kommunicate
:
import Kommunicate
Then, in the application:didFinishLaunchingWithOptions:
method, setup Kommunicate
as shown below
Kommunicate.setup(applicationId: <APP_ID>)
func application(
_: UIApplication,
didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
Kommunicate.setup(applicationId: <APP_ID>)
return true
}
In case of 'no such module' error, check the following troubleshooting intsruction:
Once you complete the pod installation, close the project and open .xcworkspace
file then build the project to avoid the above mentioned error.
Registration/Login
Convenient methods are present in Kommunicate class to register a user on Kommunicate.
Currently we support two different types of users on our iOS SDK:
1. Visitors
To register a user as a visitor to the Kommunicate server, follow these steps:
- Create a Visitor User Object
- Generate a
KMUser
object with a random user ID using thecreateVisitorUser
method from theKommunicate
class:
- Generate a
let kmUser = Kommunicate.createVisitorUser()
kmUser.applicationId = <APP_ID> // pass your Kommunicate Application ID
- Register the User as a Visitor
- Register the user as a visitor on the Kommunicate server using the
registerUserAsVisitor
method from theKommunicate
class:
- Register the user as a visitor on the Kommunicate server using the
Kommunicate.registerUserAsVisitor(kmUser, completion: { response, error in
guard error == nil else { return }
print("Success")
})
2. Pre chat Lead Collection
To collect user contact information before initiating a chat, you can show the Pre-Chat view using the following steps:
- Show the Pre-Chat View
- Initialize and configure the
KMPreChatFormViewController
:
- Initialize and configure the
let preChatVC = KMPreChatFormViewController(configuration: Kommunicate.defaultConfiguration)
preChatVC.delegate = self // Set the delegate to self to receive callbacks
preChatVC.preChatConfiguration.optionsToShow = [.email, .name, .phoneNumber] // Configure options to be visible in pre-chat
preChatVC.preChatConfiguration.mandatoryOptions = [.email, .name, .phoneNumber] // Configure mandatory options
self.present(preChatVC, animated: false, completion: nil) // Present the pre-chat view
- Handle Callbacks
- Confirm your
ViewController
to theKMPreChatFormViewControllerDelegate
to handle user actions when they tap the submit or close button:
- Confirm your
extension ViewController: KMPreChatFormViewControllerDelegate {
func userSubmittedResponse(name: String, email: String, phoneNumber: String, password: String) {
self.dismiss(animated: false, completion: nil)
let kmUser = KMUser()
kmUser.userId = <pass a unique key> // Create a Unique ID for each user. You can use this Id to perform login.
kmUser.applicationId = <APP_ID> // pass your Kommunicate Application ID
kmUser.displayName = name
kmUser.email = email
kmUser.contactNumber = phoneNumber
Kommunicate.registerUser(kmUser, completion: { response, error in
guard error == nil else { return }
print("Success")
})
}
func closeButtonTapped() {
self.dismiss(animated: false, completion: nil)
}
}
3. Registered User
If the user is logged into your app, you can pass the user information as follows:
To register a user to the Kommunicate server, use the method from the Kommunicate
class:
- Create a
KMUser
Object- Initialize a
KMUser
object and set its properties:
- Initialize a
let kmUser = KMUser()
kmUser.userId = <pass a unique key> // Create a Unique ID for each user. You can use this Id to perform login.
kmUser.applicationId = <APP_ID> // pass your Kommunicate Application ID
kmUser.displayName = displayName
kmUser.email = <pass user's emailId> // Optional
- Register the User
- Pass the
KMUser
object to theregisterUser
method to register the user on the Kommunicate server. This API can also be used for login:
- Pass the
Kommunicate.registerUser(kmUser, completion: { response, error in
guard error == nil else { return }
print("Success")
})
Pass additional details as metadata
Sometimes, you may need to pass additional details for the user apart from the already existing properties of KMUser. You can pass the additional details in metadata of the KMUser object.
// This is example data. You can set it according to the additional details you want to add
let userMetadata = NSMutableDictionary()
userMetadata["department"] = "Engineering"
userMetadata["designation"] = "Software Engineer"
userMetadata["team"] = "Device Team"
kmUser.metadata = userMetadata
To check if user is already logged in, use below API:
if Kommunicate.isLoggedIn {
// User is already logged in
}
Note: To avoid calling in registration everytime use
isLoggedIn
to check if the user is already logged in or not.
What Next?
- Check out the Conversation Section where you will get the details for creating and launching a conversation.
- Enable Push Notifications to get real-time updates.