Configure the SDK

The configuration example in this section uses the MarketingCloudSDK ConfigBuilder configuration method. This method is the most flexible way to use the Mobile App Messaging SDK.

All method names contain the prefix sfmc_. Using this prefix helps avoid namespace collisions between the external libraries it uses. For more information, see Apple Developer Documentation: Customizing Existing Classes.

Configure the SDK in your application using mamAppID, mamAccessToken, mamServerURL, and mamTenantId.

This example imports the SFMCSDK module, which is included automatically with the dependencies for the SDK. Never install this dependency manually (such as by using CocoaPods).

Important

1import MobileAppMessagingSDK
2// The SFMCSDK module is included in the dependency packages for the SDK. Don't install it
3// separately or list it in your podfile.
4import SFMCSDK
5
6class AppDelegate: UIResponder, UIApplicationDelegate {
7
8  var window: UIWindow?
9
10  // MobileAppMessaging Configuration
11  let mamAppID = YOUR_APP_ID
12  let mamAccessToken = YOUR_ACCESS_TOKEN
13  let mamServerURL = YOUR_SERVER_URL
14  let mamTenantId = YOUR_TENANT_ID
15  let mamAnalyticsEnabled = true
16
17  // MobilePush SDK: REQUIRED IMPLEMENTATION
18  @discardableResult
19
20  func configureSdk() -> Bool {
21
22    // Enable logging for debugging early on. Debug level is not recommended for production apps, as significant data
23    // about the SDK will be logged to the console.
24
25    #if DEBUG
26      SFMCSdk.setLogger(logLevel: .debug)
27    #endif
28
29    // Use the `MobileAppMessagingConfigBuilder` to configure the Mobile App Messaging SDK. This gives you the maximum flexibility in SDK configuration.
30    // The builder lets you configure the module parameters at runtime.
31
32    let mamConfiguration = MobileAppMessagingConfigBuilder(appId: mamAppID)
33      .setAccessToken(mamAccessToken)
34      .setMAMUrl(URL(string: mamServerURL)!)
35      .setTenantId(mamTenantId)
36      .setAnalyticsEnabled(mamAnalyticsEnabled)
37      .build()
38
39    // Set the completion handler to take action when all modules initialization is completed.
40    // Seting the completion handler is optional.
41
42    let completionHandler: ((_ status: [ModuleInitStatus]) -> Void) = { [weak self] status in
43      DispatchQueue.main.async {
44        self?.handleSDKInitializationCompletion(status: status)
45      }
46    }
47
48    SFMCSdk.initializeSdk(
49      ConfigBuilder().setMAM(config: mamConfiguration).build(), completion: completionHandler)
50
51    return true
52  }
53
54  // MARK: - SDK Initialization Completion Handler
55
56  private func handleSDKInitializationCompletion(status: [ModuleInitStatus]) {
57    var allSuccessful = true
58
59    for moduleStatus in status {
60      print(
61        "Module: \(moduleStatus.moduleName.rawValue), Status: \(moduleStatus.initStatus.rawValue)")
62
63      if moduleStatus.initStatus == .success {
64        // Handle successful initialization for each module
65        switch moduleStatus.moduleName {
66        case .mobileAppMessaging:
67          // Handle successful initialization for Mobile App Messaging module
68        default:
69          break
70        }
71      } else if moduleStatus.initStatus == .error {
72        allSuccessful = false
73        // module failed to initialize, check logs for more details
74      } else if moduleStatus.initStatus == .cancelled {
75        allSuccessful = false
76        // module initialization was cancelled (for example, if re-configuration
77        // was triggered before init completed)
78      } else if moduleStatus.initStatus == .timeout {
79        allSuccessful = false
80        // module failed to initialize due to timeout, check logs for more details
81      }
82    }
83    if allSuccessful {
84      print("SDK initialization completed successfully")
85    } else {
86      print("SDK initialization completed with errors - check logs above")
87    }
88  }
89
90  // MobilePush SDK: REQUIRED IMPLEMENTATION
91  func application(
92    _ application: UIApplication,
93    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
94  ) -> Bool {
95    self.configureSdk()
96    return true
97  }
98
99  // MobilePush SDK: OPTIONAL IMPLEMENTATION (if using Data Protection)
100  func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication) {
101    self.configureSdk()
102  }
103}

iOS Data Protection affects the SDK as described in this table.

iOS Data Protection LevelSDK Behavior
No protectionSDK works in the foreground and background.
Complete until first user authenticationSDK works in the foreground and background after first unlock.
Complete unless openSDK works in the foreground and background after first unlock.
CompleteSDK works only in the foreground after the device is unlocked.