1// Marketing Cloud SDK Configuration
2let mcAppID = "YOUR_MARKETING_CLOUD_APP_ID"
3let mcAccessToken = "YOUR_MARKETING_CLOUD_ACCESS_TOKEN"
4let mcServerURL = "YOUR_MARKETING_CLOUD_SERVER_URL"
5let mcMid = "YOUR_MARKETING_CLOUD_MID"
6
7// Define features of Marketing Cloud your app will use.
8let mcInboxEnabled = false
9let mcLocationEnabled = false
10let mcAnalyticsEnabled = true
11
12// MarketingCloud SDK: REQUIRED IMPLEMENTATION
13@discardableResult
14
15func configureSdk() -> Bool {
16
17 var configBuilder = ConfigBuilder()
18
19 // Enable logging for debugging early on. Debug level is not recommended for production apps, as significant data
20 // about the SDK will be logged to the console.
21
22 #if DEBUG
23 SFMCSdk.setLogger(logLevel: .debug)
24 #endif
25
26 // Use the `MarketingCloudSdkConfigBuilder` to configure the MarketingCloud SDK. This gives you the maximum flexibility in SDK configuration.
27 // The builder lets you configure the module parameters at runtime.
28
29 let engagementConfiguration = MarketingCloudSdkConfigBuilder(appId: mcAppID)
30 .setAccessToken(mcAccessToken)
31 .setMarketingCloudServerUrl(URL(string: mcServerURL)!)
32 .setMid(mcMid)
33 .setInboxEnabled(mcInboxEnabled)
34 .setLocationEnabled(mcLocationEnabled)
35 .setAnalyticsEnabled(mcAnalyticsEnabled)
36 .build()
37
38 configBuilder = configBuilder
39 .setEngagement(config: engagementConfiguration)
40
41 let pushFeatureConfiguration = PushFeatureConfigBuilder()
42 .setApplicationControlsBadging(true)
43 .build()
44
45 configBuilder = configBuilder
46 .setPushFeature(config: pushFeatureConfiguration)
47
48 // Set the completion handler to take action when all modules initialization is completed.
49 // Seting the completion handler is optional.
50
51 let completionHandler: ((_ status: [ModuleInitStatus]) -> Void) = { [weak self] status in
52 DispatchQueue.main.async {
53 self?.handleSDKInitializationCompletion(status: status)
54 }
55 }
56
57 SFMCSdk.initializeSdk(configBuilder.build(), completion: completionHandler)
58
59 return true
60}
61
62// MARK: - SDK Initialization Completion Handler
63
64private func handleSDKInitializationCompletion(status: [ModuleInitStatus]) {
65 var allSuccessful = true
66
67 for moduleStatus in status {
68 print("Module: \(moduleStatus.moduleName.rawValue), Status: \(moduleStatus.initStatus.rawValue)")
69
70 if moduleStatus.initStatus == .success {
71 // Handle successful initialization for each module
72 switch moduleStatus.moduleName {
73 case .engagement:
74 // Handle successful initialization for Marketing cloud module
75 case .pushFeature:
76 // Handle successful initialization for Push Feature module
77 default:
78 break
79 }
80 } else if moduleStatus.initStatus == .error {
81 allSuccessful = false
82 // module failed to initialize, check logs for more details
83 } else if moduleStatus.initStatus == .cancelled {
84 allSuccessful = false
85 // module initialization was cancelled (for example due to re-confirguration triggered before init was completed)
86 } else if moduleStatus.initStatus == .timeout {
87 allSuccessful = false
88 // module failed to initialize due to timeout, check logs for more details
89 }
90 }
91 if allSuccessful {
92 print("SDK initialization completed successfully")
93 } else {
94 print("SDK initialization completed with errors - check logs above")
95 }
96}