Hosting AE SDK in your iOS app
In this guide you will learn how to integrate the AE SDK in your iOS app. The SDK supports showing starting a chat session with an expert and handling everything around it, including sending and receiving images and videos as well as handling notifications. By the end of this guide you're going to have a running implementation in your app!
Currently we support iOS 11 and above.
We don't support SwiftUI.
1. Cocoapods
Contact the SDK team (via email or slack) to get a AESDK.podspec
file.
Add the AESDK.podspec
file to your repository.
Create a Podfile if you don't already have one:
$ cd your-project-directory
$ pod init
To your Podfile add the SDK pod under your main app's target:
pod 'AESDK', :podspec => './AESDK.podspec'
Go back to the terminal and run:
$ pod install
2. Configuring the SDK
These are the basic parameters you need to provide:
- Push Notification Token to support chat message notifications
- JWT Authentication Token in order for the SDK to identify and authenticate the user
- Your SDK App Key. If you don't have one, contact the SDK team via email or slack
The SDK App Key is used by the AE platform to determine different features related to your integration, for example the color scheme that will be used or which group of experts will support your users.
Follow these steps to set the app key, auth token and push notifications token:
- Open your
.xcworkspace
file with Xcode
$ open your-project.xcworkspace
- Make sure that your app registers for remote notifications. You should have something like this in your app:
func applicationDidBecomeActive(_ application: UIApplication) {
let center = UNUserNotificationCenter.current()
// Request permission to display alerts and play sounds.
center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
// Enable or disable features based on authorization.
}
UIApplication.shared.registerForRemoteNotifications()
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
UNUserNotificationCenter *notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
[notificationCenter requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
[application registerForRemoteNotifications];
}
At the top of the file that will contain the configuration code written in you should import the SDK:
import AESDK
#import <AESDK/Sdk.h>
- Than call the
configureAppKey:withAuthToken:withApnsToken:withBundleId:
class function on the globalAESDK
object:
AESDK.configureAppKey(appKey, withAuthToken: authJWT, withApnsToken: notificationsToken, withBundleId: bundleId)
[AESDK configureAppKey: appKey withAuthToken: authJWT withApnsToken: notificationsToken withBundleId: bundleId];
3. Enabling push notifications
The Anywhere Expert platform supports sending push notifications using APNs. We use Apple's AuthKey method to create tokens for Apple's push notifications services. That requires you to provide us with your AuthKey which is a .p8 file generated through the developer portal. Learn how to obtain a key here. A notification will be displayed to the user in 2 cases:
- The app is in the background.
- The app is in the foreground but not in the chat view.
4. Handling push notifications
The app will receive a push notification whenever the service sends a message to the user. The SDK can handle these notifications by popping up the chat view and showing the latest message. To enable this behavior you need to add the following code to your notifications handlers:
To handle push notifications when the app is in the foreground, in your forground notifications handler add this code:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if (!AESDK.handleForegroundPushNotification(userInfo, withCompletionHandler: completionHandler)) {
// It's not AE notification - handle it yourself
}
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
if (![AESDK handleForegroundPushNotification:userInfo withCompletionHandler: completionHandler]) {
// It's not AE notification - handle it yourself
}
}
To handle notifications when the app is in the background, in your background notifications handler (not to be confused with silent notifications/background fetch) add the following code:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if (!AESDK.handlePushNotification(userInfo)) {
// It's not AE notification - handle it yourself
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
if (![AESDK handlePushNotification:userInfo]) {
// It's not AE notification - handle it yourself
}
}
5. Starting a chat session
All you have to do is call a single function:
AESDK.showChatView()
[self showChatView]
When you call this function a view with a chat should appear. You know everything works if you're able to start a chat session with an expert.