Manual Lifecycle implementation
In Adobe Mobile Library (v4) iOS SDK, this implementation was completed automatically.
When upgrading to the Experience Platform SDK, you must add code to continue collecting Lifecycle metrics.
Import and register the Lifecycle extension
Java
Import the Lifecycle library:
Copied to your clipboardimport com.adobe.marketing.mobile.*;
Register the Lifecycle extension along with other extensions used with Mobile Core:
Copied to your clipboardpublic class MyApp extends Application {@Overridepublic void onCreate() {super.onCreate();MobileCore.setApplication(this);MobileCore.registerExtensions(Arrays.asList(Lifecycle.EXTENSION,...), value -> {// registration completion handler});}}
Swift
Copied to your clipboardimport AEPCoreimport AEPLifecycle
Register the Lifecycle extension along with the other extensions you use with the Mobile Core by adding the following in your app's application(_:didFinishLaunchingWithOptions:)
delegate method:
Copied to your clipboardfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {MobileCore.registerExtensions([Lifecycle.self, ...], {// Use the environment file id assigned to this application in Data Collection UIMobileCore.configureWith(appId: "your-environment-file-id")})return true}
Objective-C
Copied to your clipboard@import AEPCore;@import AEPLifecycle;
Register the Lifecycle extension along with the other extensions you use with the Mobile Core by adding the following to your app's application:didFinishLaunchingWithOptions:
delegate method:
Copied to your clipboard- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {[AEPMobileCore registerExtensions:@[AEPMobileLifecycle.class, ...] completion:^{// Use the environment file id assigned to this application in Data Collection UI[AEPMobileCore configureWithAppId: @"your-environment-file-id"];}];return YES;}
Java
Import the Lifecycle library:
Copied to your clipboardimport com.adobe.marketing.mobile.*;
Register the Lifecycle extension along with other extensions used with Mobile Core:
Copied to your clipboardpublic class MyApp extends Application {@Overridepublic void onCreate() {super.onCreate();MobileCore.setApplication(this);MobileCore.registerExtensions(Arrays.asList(Lifecycle.EXTENSION,...), value -> {// registration completion handler});}}
Swift
Copied to your clipboardimport AEPCoreimport AEPLifecycle
Register the Lifecycle extension along with the other extensions you use with the Mobile Core by adding the following in your app's application(_:didFinishLaunchingWithOptions:)
delegate method:
Copied to your clipboardfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {MobileCore.registerExtensions([Lifecycle.self, ...], {// Use the environment file id assigned to this application in Data Collection UIMobileCore.configureWith(appId: "your-environment-file-id")})return true}
Objective-C
Copied to your clipboard@import AEPCore;@import AEPLifecycle;
Register the Lifecycle extension along with the other extensions you use with the Mobile Core by adding the following to your app's application:didFinishLaunchingWithOptions:
delegate method:
Copied to your clipboard- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {[AEPMobileCore registerExtensions:@[AEPMobileLifecycle.class, ...] completion:^{// Use the environment file id assigned to this application in Data Collection UI[AEPMobileCore configureWithAppId: @"your-environment-file-id"];}];return YES;}
Start collecting Lifecycle information
You can start collecting Lifecycle information at any time in your app, but we recommend that you start as soon as your app enters the foreground. This allows Lifecycle metrics to be correctly attributed to all of your users' activities for their current session.
Java
Do not start or stop Lifecycle in a Fragment.
In the onResume
function of each of your Activities, start Lifecycle data collection:
Copied to your clipboard@Overridepublic void onResume() {MobileCore.setApplication(getApplication());MobileCore.lifecycleStart(null);}
To ensure accurate session and crash reporting, this call must be added to every Activity.
Setting the application is only necessary on activities that are entry points for your application. However, setting the application on each Activity has no negative impact and ensures that the SDK always has the necessary reference to your application. We recommend that you call setApplication
in each of your Activities.
Swift
Start Lifecycle data collection by calling lifecycleStart(additionalContextData:)
from the registration completion handler in your app's application(_:didFinishLaunchingWithOptions:)
delegate method.
If your iOS application supports background capabilities, your application(_:didFinishLaunchingWithOptions:)
method might be called when iOS launches your app in the background. If you do not want background launches to count towards your lifecycle metrics, lifecycleStart(additionalContextData:)
should only be called when the application state is not equal to UIApplication.State.background
.
Copied to your clipboardfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {let appState = application.applicationStateMobileCore.registerExtensions([Lifecycle.self, ...], {// Use the environment file id assigned to this application in Data Collection UIMobileCore.configureWith(appId: "your-environment-file-id")// only start lifecycle if the application is not in the backgroundif appState != .background {MobileCore.lifecycleStart(additionalContextData: nil)}})}
When your app is launched, if it is resuming from a backgrounded state, iOS might call your applicationWillEnterForeground:
delegate method. You also need to call lifecycleStart:
, but this time you do not need all of the supporting code that you used in application:didFinishLaunchingWithOptions:
:
Copied to your clipboardfunc applicationWillEnterForeground(_ application: UIApplication) {MobileCore.lifecycleStart(additionalContextData: nil)}
If your app is a SceneDelegate based iOS application, then use:
Copied to your clipboardfunc sceneWillEnterForeground(_ scene: UIScene) {MobileCore.lifecycleStart(additionalContextData: nil)}
Objective-C
Start Lifecycle data collection by calling lifecycleStart:
from within the callback of the ACPCore::start:
method in your app's application:didFinishLaunchingWithOptions:
delegate method.
If your iOS application supports background capabilities, your application:didFinishLaunchingWithOptions:
method might be called when iOS launches your app in the background. If you do not want background launches to count towards your lifecycle metrics, lifecycleStart:
should only be called when the application state is not equal to UIApplicationStateBackground
.
Copied to your clipboard- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {const UIApplicationState appState = application.applicationState;[AEPMobileCore registerExtensions:@[AEPMobileLifecycle.class, ...] completion:^{// Use the environment file id assigned to this application in Data Collection UI[AEPMobileCore configureWithAppId: @"your-environment-file-id"];// only start lifecycle if the application is not in the backgroundif (appState != UIApplicationStateBackground) {[AEPMobileCore lifecycleStart:nil];}}];return YES;}
When your app is launched, if it is resuming from a backgrounded state, iOS might call your applicationWillEnterForeground:
delegate method. You also need to call lifecycleStart:
, but this time you do not need all of the supporting code that you used in application:didFinishLaunchingWithOptions:
:
Copied to your clipboard- (void) applicationWillEnterForeground:(UIApplication *)application {[AEPMobileCore lifecycleStart:nil];}
If your app is a SceneDelegate based iOS application, then use:
Copied to your clipboard- (void) sceneWillEnterForeground:(UIScene *)scene {[AEPMobileCore lifecycleStart:nil];}
Java
Do not start or stop Lifecycle in a Fragment.
In the onResume
function of each of your Activities, start Lifecycle data collection:
Copied to your clipboard@Overridepublic void onResume() {MobileCore.setApplication(getApplication());MobileCore.lifecycleStart(null);}
To ensure accurate session and crash reporting, this call must be added to every Activity.
Setting the application is only necessary on activities that are entry points for your application. However, setting the application on each Activity has no negative impact and ensures that the SDK always has the necessary reference to your application. We recommend that you call setApplication
in each of your Activities.
Swift
Start Lifecycle data collection by calling lifecycleStart(additionalContextData:)
from the registration completion handler in your app's application(_:didFinishLaunchingWithOptions:)
delegate method.
If your iOS application supports background capabilities, your application(_:didFinishLaunchingWithOptions:)
method might be called when iOS launches your app in the background. If you do not want background launches to count towards your lifecycle metrics, lifecycleStart(additionalContextData:)
should only be called when the application state is not equal to UIApplication.State.background
.
Copied to your clipboardfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {let appState = application.applicationStateMobileCore.registerExtensions([Lifecycle.self, ...], {// Use the environment file id assigned to this application in Data Collection UIMobileCore.configureWith(appId: "your-environment-file-id")// only start lifecycle if the application is not in the backgroundif appState != .background {MobileCore.lifecycleStart(additionalContextData: nil)}})}
When your app is launched, if it is resuming from a backgrounded state, iOS might call your applicationWillEnterForeground:
delegate method. You also need to call lifecycleStart:
, but this time you do not need all of the supporting code that you used in application:didFinishLaunchingWithOptions:
:
Copied to your clipboardfunc applicationWillEnterForeground(_ application: UIApplication) {MobileCore.lifecycleStart(additionalContextData: nil)}
If your app is a SceneDelegate based iOS application, then use:
Copied to your clipboardfunc sceneWillEnterForeground(_ scene: UIScene) {MobileCore.lifecycleStart(additionalContextData: nil)}
Objective-C
Start Lifecycle data collection by calling lifecycleStart:
from within the callback of the ACPCore::start:
method in your app's application:didFinishLaunchingWithOptions:
delegate method.
If your iOS application supports background capabilities, your application:didFinishLaunchingWithOptions:
method might be called when iOS launches your app in the background. If you do not want background launches to count towards your lifecycle metrics, lifecycleStart:
should only be called when the application state is not equal to UIApplicationStateBackground
.
Copied to your clipboard- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {const UIApplicationState appState = application.applicationState;[AEPMobileCore registerExtensions:@[AEPMobileLifecycle.class, ...] completion:^{// Use the environment file id assigned to this application in Data Collection UI[AEPMobileCore configureWithAppId: @"your-environment-file-id"];// only start lifecycle if the application is not in the backgroundif (appState != UIApplicationStateBackground) {[AEPMobileCore lifecycleStart:nil];}}];return YES;}
When your app is launched, if it is resuming from a backgrounded state, iOS might call your applicationWillEnterForeground:
delegate method. You also need to call lifecycleStart:
, but this time you do not need all of the supporting code that you used in application:didFinishLaunchingWithOptions:
:
Copied to your clipboard- (void) applicationWillEnterForeground:(UIApplication *)application {[AEPMobileCore lifecycleStart:nil];}
If your app is a SceneDelegate based iOS application, then use:
Copied to your clipboard- (void) sceneWillEnterForeground:(UIScene *)scene {[AEPMobileCore lifecycleStart:nil];}
Pause Lifecycle collection
You should pause Lifecycle collection when the user stops using your app. The best time to do this is usually when your app has entered the background.
Java
Do not start or stop Lifecycle in a Fragment.
We recommend pausing Lifecycle from the onPause
function in your Activities:
Copied to your clipboard@Overridepublic void onPause() {MobileCore.lifecyclePause();}
To ensure accurate session and crash reporting, this call must be added to every Activity.
Swift
When the app enters the background, pause Lifecycle data collection from your app's applicationDidEnterBackground:
delegate method:
Copied to your clipboardfunc applicationDidEnterBackground(_ application: UIApplication) {MobileCore.lifecyclePause()}
If your app is a SceneDelegate based iOS application, then use:
Copied to your clipboardfunc sceneDidEnterBackground(_ scene: UIScene) {MobileCore.lifecyclePause()}
Objective-C
When the app enters the background, pause Lifecycle data collection from your app's applicationDidEnterBackground:
delegate method:
Copied to your clipboard- (void) applicationDidEnterBackground:(UIApplication *)application {[AEPMobileCore lifecyclePause];}
If your app is a SceneDelegate based iOS application, then use:
Copied to your clipboard- (void) sceneDidEnterBackground:(UIScene *)scene {[AEPMobileCore lifecyclePause];}
Java
Do not start or stop Lifecycle in a Fragment.
We recommend pausing Lifecycle from the onPause
function in your Activities:
Copied to your clipboard@Overridepublic void onPause() {MobileCore.lifecyclePause();}
To ensure accurate session and crash reporting, this call must be added to every Activity.
Swift
When the app enters the background, pause Lifecycle data collection from your app's applicationDidEnterBackground:
delegate method:
Copied to your clipboardfunc applicationDidEnterBackground(_ application: UIApplication) {MobileCore.lifecyclePause()}
If your app is a SceneDelegate based iOS application, then use:
Copied to your clipboardfunc sceneDidEnterBackground(_ scene: UIScene) {MobileCore.lifecyclePause()}
Objective-C
When the app enters the background, pause Lifecycle data collection from your app's applicationDidEnterBackground:
delegate method:
Copied to your clipboard- (void) applicationDidEnterBackground:(UIApplication *)application {[AEPMobileCore lifecyclePause];}
If your app is a SceneDelegate based iOS application, then use:
Copied to your clipboard- (void) sceneDidEnterBackground:(UIScene *)scene {[AEPMobileCore lifecyclePause];}