Earlier versions of the SDK supported JSON file configuration, which is deprecated. If your implementation uses JSON configuration, reconfigure it using the builder method.
Note
Use these code examples to configure the SDK in your app. Make these changes to the examples:
If your app uses version 10 or later of the SDK, use this code.
1import MarketingCloudSDK2import SFMCSDK3import UIKit45@main6class AppDelegate: UIResponder, UIApplicationDelegate {78 var window: UIWindow?910 // Marketing Cloud SDK configuration11 let mcAppID = "<your MC appID here>"12 let mcAccessToken = "<your MC accessToken here>"13 let mcServerURL = "<your mc serverURL here>"14 let mcMid = "<your account MID here>"1516 // Define features of Marketing Cloud your app uses17 let mcInboxEnabled = false18 let mcLocationEnabled = false19 let mcAnalyticsEnabled = true2021 // MarketingCloud SDK: REQUIRED IMPLEMENTATION22 @discardableResult2324 func configureSdk() ->Bool{2526 // Enable logging for debugging. Don't use debug level in production apps because the SDK logs27 // significant data to the console.2829 #if DEBUG30 SFMCSdk.setLogger(logLevel: .debug)31 #endif3233 // Use the `MarketingCloudSdkConfigBuilder` to configure the MarketingCloud SDK.34 // The builder lets you configure the module parameters at runtime.3536 let engagementConfiguration = MarketingCloudSdkConfigBuilder(appId: mcAppID)37 .setAccessToken(mcAccessToken)38 .setMarketingCloudServerUrl(URL(string: mcServerURL)!)39 .setMid(mcMid)40 .setInboxEnabled(mcInboxEnabled)41 .setLocationEnabled(mcLocationEnabled)42 .setAnalyticsEnabled(mcAnalyticsEnabled)43 .build()4445 // Set the completion handler to take action when module initialization completes.46 // Setting the completion handler is optional.4748 let completionHandler: ((_ status: [ModuleInitStatus]) ->Void) = {[weak self] status in49 DispatchQueue.main.async{50 self?.handleSDKInitializationCompletion(status: status)51}52}5354 SFMCSdk.initializeSdk(55 ConfigBuilder().setEngagement(config: engagementConfiguration).build(),56 completion: completionHandler)5758 return true59}6061 // MARK: - SDK Initialization Completion Handler6263 private func handleSDKInitializationCompletion(status: [ModuleInitStatus]){64 var allSuccessful = true6566 for moduleStatus in status {67 print(68 "Module: \(moduleStatus.moduleName.rawValue), Status: \(moduleStatus.initStatus.rawValue)")6970 if moduleStatus.initStatus == .success {71 // Handle successful initialization for each module72 switch moduleStatus.moduleName {73 case .engagement:74 // Handle successful initialization for Marketing Cloud module75 default:76 break77}78}else if moduleStatus.initStatus == .error{79 allSuccessful = false80 // Module failed to initialize. Check logs for details.81}else if moduleStatus.initStatus == .cancelled {82 allSuccessful = false83 // Module initialization was cancelled. For example, reconfiguration was triggered before initialization completed.84}else if moduleStatus.initStatus == .timeout {85 allSuccessful = false86 // Module failed to initialize due to timeout. Check logs for details.87}88}89 if allSuccessful {90 print("SDK initialization completed successfully")91}else{92 print("SDK initialization completed with errors - check logs above")93}94}9596 func application(97 _ application: UIApplication,98 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?99) ->Bool{100 // Override point for customization after application launch.101 self.configureSdk()102 return true103}104105 // Marketing Cloud SDK: OPTIONAL IMPLEMENTATION (if using Data Protection)106 func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication){107 self.configureSdk()108}109}
If your app uses version 8 or 9 of the SDK, use this code.
1import SFMCSDK2import MarketingCloudSDK34class AppDelegate: UIResponder, UIApplicationDelegate {56 var window: UIWindow?78 // SDK: REQUIRED IMPLEMENTATION910 // The appID, accessToken, and appEndpoint are required configuration values.11 #if DEBUG12 let appId = YOUR_DEV_APP_ID13 let accessToken = YOUR_DEV_ACCESS_TOKEN14 let appEndpoint = YOUR_DEV_APP_ENDPOINT15 let mid = YOUR_DEV_MID16 #else17 let appId = YOUR_PROD_APP_ID18 let accessToken = YOUR_PROD_ACCESS_TOKEN19 let appEndpoint = YOUR_PROD_APP_ENDPOINT20 let mid = YOUR_PROD_MID21 #endif222324 // Define the features your app uses25 let inbox = false26 let location = false27 let analytics = true2829 // SDK: REQUIRED IMPLEMENTATION30 func configureSDK(){31 // Enable logging for debugging. Don't use debug level in production apps because the SDK logs32 // a large amount of data to the console.33 #if DEBUG34 SFMCSdk.setLogger(logLevel: .debug)35 #endif3637 // Use the Config Builder to configure the module parameters at runtime.38 let mobilePushConfiguration = PushConfigBuilder(appId: appId)39 .setAccessToken(accessToken)40 .setMarketingCloudServerUrl(appEndpoint)41 .setMid(mid)42 .setInboxEnabled(inbox)43 .setLocationEnabled(location)44 .setAnalyticsEnabled(analytics)45 .build()4647 // Set the completion handler to take action when module initialization completes. The result indicates whether initialization was successful.48 // Setting the completion handler is optional.49 let completionHandler: (OperationResult) ->() = { result in50 if result == .success {51 // Module is fully configured and ready for use52}else if result == .error{53 // Module failed to initialize. Check logs for details.54}else if result == .cancelled {55 // Module initialization was cancelled. For example, reconfiguration was triggered before initialization completed.56}else if result == .timeout {57 // Module failed to initialize due to timeout. Check logs for details.58}59}6061 // After you create the configuration, initialize the SDK.62 SFMCSdk.initializeSdk(ConfigBuilder().setPush(config: mobilePushConfiguration, onCompletion: completionHandler).build())63}6465 // SDK: REQUIRED IMPLEMENTATION66 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) ->Bool{67 self.configureSDK()6869 return true70}7172 // SDK: OPTIONAL IMPLEMENTATION (if using Data Protection)73 func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication){74 if(SFMCSdk.mp.getStatus() != .operational){75 self.configureSFMCSdk()76}77}78}
If your app uses version 7 of the SDK, use this code.
1import MarketingCloudSDK23class AppDelegate: UIResponder, UIApplicationDelegate {45 var window: UIWindow?67 // REQUIRED IMPLEMENTATION89 // The appID, accessToken, and appEndpoint are required configuration values.1011 // Use the builder method to configure the SDK.12 // The builder lets you configure the SDK parameters at runtime.13 #if DEBUG1415 let appID = YOUR_DEV_APP_ID16 let accessToken = YOUR_DEV_ACCESS_TOKEN17 let appEndpoint = YOUR_DEV_APP_ENDPOINT18 let mid = YOUR_DEV_MID19 #else20 let appId = YOUR_PROD_APP_ID21 let accessToken = YOUR_PROD_ACCESS_TOKEN22 let appEndpoint = YOUR_PROD_APP_ENDPOINT23 let mid = YOUR_PROD_MID24 #endif252627 // Define the features your app uses28 let inbox = false29 let location = false30 let analytics = true3132 // REQUIRED IMPLEMENTATION33 @discardableResult34 func configureMarketingCloudSDK() ->Bool{35 // Use the builder method to configure the SDK.36 // The builder lets you configure the SDK parameters at runtime.37 let builder = MarketingCloudSDKConfigBuilder()38 .sfmc_setApplicationId(appID)39 .sfmc_setAccessToken(accessToken)40 .sfmc_setMarketingCloudServerUrl(appEndpoint)41 .sfmc_setMid(mid)42 .sfmc_setInboxEnabled(inbox as NSNumber)43 .sfmc_setLocationEnabled(location as NSNumber)44 .sfmc_setAnalyticsEnabled(analytics as NSNumber)45 .sfmc_build()!4647 var success = false4849 // After you create the builder, pass it to the sfmc_configure method.50 do{51 try MarketingCloudSDK.sharedInstance().sfmc_configure(with:builder)52 success = true53}catch let error as NSError{54 // Configuration errors are returned in the NSError parameter. Use them to determine55 // whether you implemented the SDK correctly.5657 let configErrorString = String(format: "MarketingCloudSDK sfmc_configure failed with error = %@", error)58 print(configErrorString)59}6061 if success == true{62 // The SDK is fully configured and ready for use.6364 // Enable logging for debugging. Don't use this in production apps because the SDK logs65 // a large amount of data to the console.66 #if DEBUG67 MarketingCloudSDK.sharedInstance().sfmc_setDebugLoggingEnabled(true)68 #endif69}7071 return success72}7374 // REQUIRED IMPLEMENTATION75 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) ->Bool{76 return self.configureMarketingCloudSDK()77}7879 // OPTIONAL IMPLEMENTATION (if using Data Protection)80 func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication){81 if(MarketingCloudSDK.sharedInstance().sfmc_isReady() == false)82{83 self.configureMarketingCloudSDK()84}85}86}
Data Protection Considerations
iOS Data Protection encrypts files stored on the device and controls when your app can access them based on the lock state of the user’s device. If your app uses Data Protection, you must configure the SDK at the right time to ensure it can access the encrypted data it needs to function. The SDK’s ability to work in the foreground or background depends on the Data Protection level you configure.
iOS Data Protection affects the SDK in these ways.
iOS Data Protection Level
SDK Behavior
No protection
SDK works in the foreground and background
Complete until first user authentication
SDK works in the foreground and background after first unlock
Complete unless open
SDK works in the foreground and background after first unlock
Complete
SDK works only in the foreground after the device is unlocked