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:
These examples import the SFMCSDK module, which is included automatically with the dependencies for the SDK. Never install this dependency manually (such as by using CocoaPods).
Important
If your app uses version 10 or later of the SDK, use this code.
1import MarketingCloudSDK2// The SFMCSDK module is included in the dependency packages for the SDK. Don't install it3// separately or list it in your podfile.4import SFMCSDK5import UIKit67@main8class AppDelegate: UIResponder, UIApplicationDelegate {910 var window: UIWindow?1112 // Marketing Cloud SDK configuration13 let mcAppID = "<your MC appID here>"14 let mcAccessToken = "<your MC accessToken here>"15 let mcServerURL = "<your mc serverURL here>"16 let mcMid = "<your account MID here>"1718 // Define features of Marketing Cloud your app uses19 let mcInboxEnabled = false20 let mcLocationEnabled = false21 let mcAnalyticsEnabled = true2223 // MarketingCloud SDK: REQUIRED IMPLEMENTATION24 @discardableResult2526 func configureSdk() ->Bool{2728 // Enable logging for debugging. Don't use debug level in production apps because the SDK logs29 // significant data to the console.3031 #if DEBUG32 SFMCSdk.setLogger(logLevel: .debug)33 #endif3435 // Use the `MarketingCloudSdkConfigBuilder` to configure the MarketingCloud SDK.36 // The builder lets you configure the module parameters at runtime.3738 let engagementConfiguration = MarketingCloudSdkConfigBuilder(appId: mcAppID)39 .setAccessToken(mcAccessToken)40 .setMarketingCloudServerUrl(URL(string: mcServerURL)!)41 .setMid(mcMid)42 .setInboxEnabled(mcInboxEnabled)43 .setLocationEnabled(mcLocationEnabled)44 .setAnalyticsEnabled(mcAnalyticsEnabled)45 .build()4647 // Set the completion handler to take action when module initialization completes.48 // Setting the completion handler is optional.4950 let completionHandler: ((_ status: [ModuleInitStatus]) ->Void) = {[weak self] status in51 DispatchQueue.main.async{52 self?.handleSDKInitializationCompletion(status: status)53}54}5556 SFMCSdk.initializeSdk(57 ConfigBuilder().setEngagement(config: engagementConfiguration).build(),58 completion: completionHandler)5960 return true61}6263 // MARK: - SDK Initialization Completion Handler6465 private func handleSDKInitializationCompletion(status: [ModuleInitStatus]){66 var allSuccessful = true6768 for moduleStatus in status {69 print(70 "Module: \(moduleStatus.moduleName.rawValue), Status: \(moduleStatus.initStatus.rawValue)")7172 if moduleStatus.initStatus == .success {73 // Handle successful initialization for each module74 switch moduleStatus.moduleName {75 case .engagement:76 // Handle successful initialization for Marketing Cloud module77 default:78 break79}80}else if moduleStatus.initStatus == .error{81 allSuccessful = false82 // Module failed to initialize. Check logs for details.83}else if moduleStatus.initStatus == .cancelled {84 allSuccessful = false85 // Module initialization was cancelled. For example, reconfiguration was triggered before initialization completed.86}else if moduleStatus.initStatus == .timeout {87 allSuccessful = false88 // Module failed to initialize due to timeout. Check logs for 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}9798 func application(99 _ application: UIApplication,100 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?101) ->Bool{102 // Override point for customization after application launch.103 self.configureSdk()104 return true105}106107 // Marketing Cloud SDK: OPTIONAL IMPLEMENTATION (if using Data Protection)108 func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication){109 self.configureSdk()110}111}
If your app uses version 8 or 9 of the SDK, use this code.
1// The SFMCSDK module is included in the dependency packages for the SDK. Don't install it2// separately or list it in your podfile.3import SFMCSDK4import MarketingCloudSDK56class AppDelegate: UIResponder, UIApplicationDelegate {78 var window: UIWindow?910 // SDK: REQUIRED IMPLEMENTATION1112 // The appID, accessToken, and appEndpoint are required configuration values.13 #if DEBUG14 let appId = YOUR_DEV_APP_ID15 let accessToken = YOUR_DEV_ACCESS_TOKEN16 let appEndpoint = YOUR_DEV_APP_ENDPOINT17 let mid = YOUR_DEV_MID18 #else19 let appId = YOUR_PROD_APP_ID20 let accessToken = YOUR_PROD_ACCESS_TOKEN21 let appEndpoint = YOUR_PROD_APP_ENDPOINT22 let mid = YOUR_PROD_MID23 #endif242526 // Define the features your app uses27 let inbox = false28 let location = false29 let analytics = true3031 // SDK: REQUIRED IMPLEMENTATION32 func configureSDK(){33 // Enable logging for debugging. Don't use debug level in production apps because the SDK logs34 // a large amount of data to the console.35 #if DEBUG36 SFMCSdk.setLogger(logLevel: .debug)37 #endif3839 // Use the Config Builder to configure the module parameters at runtime.40 let mobilePushConfiguration = PushConfigBuilder(appId: appId)41 .setAccessToken(accessToken)42 .setMarketingCloudServerUrl(appEndpoint)43 .setMid(mid)44 .setInboxEnabled(inbox)45 .setLocationEnabled(location)46 .setAnalyticsEnabled(analytics)47 .build()4849 // Set the completion handler to take action when module initialization completes. The result indicates whether initialization was successful.50 // Setting the completion handler is optional.51 let completionHandler: (OperationResult) ->() = { result in52 if result == .success {53 // Module is fully configured and ready for use54}else if result == .error{55 // Module failed to initialize. Check logs for details.56}else if result == .cancelled {57 // Module initialization was cancelled. For example, reconfiguration was triggered before initialization completed.58}else if result == .timeout {59 // Module failed to initialize due to timeout. Check logs for details.60}61}6263 // After you create the configuration, initialize the SDK.64 SFMCSdk.initializeSdk(ConfigBuilder().setPush(config: mobilePushConfiguration, onCompletion: completionHandler).build())65}6667 // SDK: REQUIRED IMPLEMENTATION68 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) ->Bool{69 self.configureSDK()7071 return true72}7374 // SDK: OPTIONAL IMPLEMENTATION (if using Data Protection)75 func applicationProtectedDataDidBecomeAvailable(_ application: UIApplication){76 if(SFMCSdk.mp.getStatus() != .operational){77 self.configureSFMCSdk()78}79}80}
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