Initial SDK Setup

Step 1: Getting the 360dialog SDK

CocoaPods

To integrate the 360dialog SDK using CocoaPods:

  • Add the following snippet to your Podfile

    # Podfile
    platform :ios, '8.0'
    
    # The usage of frameworks is optional but recommended.
    # If you don't use this flag, you will have to import our SDK using
    # import <D360Kit/D360Kit.h> instead of simply @import D360Kit;
    use_frameworks!
    
    source 'git@github.com:360dialog/d360-cocoapods-specs.git'
    
    pod 'D360Kit', '~> 3.9'
    
  • Install the pods using

    pod repo update
    pod install
    

Note

The podspec uses a SSH connection to Github to allow a better integration in a CI environment so don’t forget to add your public key in your Github account. We’ve also deprecated the D360SDK in favour of D360Kit name.

Carthage

To integrate the 360dialog SDK using Carthage:

  • Add the following snippet to your Cartfile

    # Cartfile
    github "360dialog/sdk-ios" ~> 3.8
    
  • Run the carthage update --use-ssh command

  • Follow the Carthage instructions to include the $(SRCROOT)/Carthage/Build/iOS/D360Kit.framework in your application. The resulting XCode build phase setting should look like this:

../_images/ios_xcode_build_phase.png

Submodule

To integrate the 360dialog SDK using a git submodule:

  • Add the 360dialog repository to your project

    git submodule add git@github.com:360dialog/sdk-ios.git D360Kit
    
  • Drag and drop the D360Kit.xcodeproj to your Xcode .xcworkspace

  • Add the D360Kit.framework as an “Embedded Binary” in the “General” tab.

../_images/ios_xcode_submodule.png

Step 2: Integrate the SDK

To integrate the 360dialog SDK in your application, add the following code snippets to your UIApplicationDelegate. Replace the appId and the apiKey by those from your Dashboard.

Objective-C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@import D360Kit;
@import UserNotifications;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    D360Options *options = [D360Options optionsWithAppId:@"12345" apiKey:@"00000000000000000"];
    [D360 startWithOptions:options];
    
    // rest of your method
    return YES;
}

Swift

1
2
3
4
5
6
7
8
9
import D360Kit

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    let options = D360Options(appId: "12345", apiKey: "00000000000000000")
    D360.start(withOptions: options)
    
    return true
}

Note

Be sure to replace the appId and apiKey values with the ones from your application dashboard. These values can also be taken from the API.

Step 3: Implement AppDelegate Methods

For the 360dialog SDK to function correctly, you need to implement the following methods.

Note

The push notification integration is required even if you don’t plan on using push campaigns. The 360dialog SDK uses the silent push channel for internal purposes. Also note that the 360dialog SDK does not trigger the user notification permission dialog.

Push Tokens

Hand over the push token to the 360dialog SDK by implementing the application:didRegisterForRemoteNotificationsWithDeviceToken:

Objective-C

1
2
3
4
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [[D360 push] registerDeviceToken:deviceToken];
}

Swift

1
2
3
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    D360.push().registerDeviceToken(deviceToken)
}

Log push token errors in application:didFailToRegisterForRemoteNotificationsWithError:. This delegate method is not used by our SDK but we recommend to implement it and log the error for debugging purposes.

Objective-C

1
2
3
4
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"Failed to register remote notifications: %@", error);
}

Swift

1
2
3
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Failed to register remote notifications: \(error)")
}

Handle Notifications

Implement the application:didReceiveRemoteNotification:fetchCompletionHandler:. This delegate method is used to let the 360dialog SDK handle any received push notifications.

Objective-C

1
2
3
4
5
6
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
    if (![[D360 push] handleNotification:userInfo fetchCompletionHandler:completionHandler]) {
        completionHandler(UIBackgroundFetchResultNewData);
    }
}

Swift

1
2
3
4
5
6
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    if(!D360.push().handleNotification(userInfo, fetchCompletionHandler: completionHandler)) {
            completionHandler(.newData)
    }
}

Note

The [[D360 push] handleNotification:fetchCompletionHandler returns NO in case the received push was not sent from the 360dialog push service. You can use this return value in case you use other push notifications providers.

Foreground Notifications

While you are modifying your AppDelegate class, you might want to optionally integrate foreground notifications. More on that in the Foreground notification section.

Notification Categories

When the user accepts the notifications using application:didRegisterUserNotificationSettings:: In this delegate method you let our backend know which user notification settings and categories the user accepted. Please refer to User Notifications for more details about how to ask the user for notification permissions.

Objective-C

1
2
3
4
5
6
#pragma - User Notifications

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    [[D360 push] registerNotificationCategories:notificationSettings.categories];
}

Swift

1
2
3
4
5
6
7
// MARK: - User Notifications

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {

    D360.push().registerNotificationCategories(notificationSettings.categories)
    
}

Step 4: Update XCode Capabilities

The 360dialog SDK handles any Push Notifications that are received while the app is running but also when the app is in the background. Update your app’s target and enable the following capabilities:

  • Push Notifications
  • Background Modes (Remote Notifications)

The app’s capabilities should look like this

../_images/ios_xcode_capabilities.png

When you enable the Push Notifications capabilities, XCode generates automatically the .entitlements file with a aps-environment set to development. We recommend adding a another .entitlements file with where the APNS is set to production and use this file when you app is buit for Release

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>production</string>
</dict>
</plist>
../_images/ios_build_settings_entitlements.png

Step 5: Compile and run

At this point, you can compile and run the application. In the XCode console, you should see an output similar to this:

[D360 Info]: Starting SDK 3.6.0
[D360 Info]:

##################################### SDK push token #################################################
   8c23510ba6c55ef1d1a3d57e5e5c6e720694ae9a9711000000000000000000000
######################################################################################################

[D360 Info]:

##################################### SDK device id #################################################
   iyRPX67gMRL4jcbsBhhs4pDqArJN4s6f59EruBE8T9YTzhk5s0000000000000000000000000000000000000
#####################################################################################################

Note

The push token output is visible only when you run the application on a device.

Your basic SDK integration is done. For further configuration of Push Notifications and Event handling, refer to the Events and Push Notifications sections.