Edit in GitHubLog an issue

Import the library:

Copied to your clipboard
import com.adobe.marketing.mobile.*;

The Lifecycle extension is included in the Mobile Core extension. Add the Mobile Core extension to your project using Cocoapods.

Add the following pod in your Podfile:

Copied to your clipboard
pod 'ACPCore','~>2.0'

Import the Lifecycle library:

Swift

In Swift, importing ACPCore also imports the necessary Lifecycle APIs:

Copied to your clipboard
import ACPCore

Objective-C

Copied to your clipboard
#import "ACPLifecycle.h"
#import "ACPCore.h"

JavaScript

Import the Lifecycle extension

Copied to your clipboard
import {ACPLifecycle} from '@adobe/react-native-acpcore';

Dart

Import the Lifecycle extension

Copied to your clipboard
import 'package:flutter_acpcore/flutter_acplifecycle.dart';

Cordova

After creating your Cordova app and adding the Android and iOS platforms, the Lifecycle extension for Cordova can be added with this command:

Copied to your clipboard
cordova plugin add https://github.com/adobe/cordova-acpcore.git

C#

After importing the ACPCore.unitypackage, the Lifecycle extension for Unity can be added with following code in the MainScript

Copied to your clipboard
using com.adobe.marketing.mobile;

C#

After adding the iOS ACPCore NuGet package or the Android ACPLifecycle NuGet package, the Lifecycle extension can be added by this import statement

Copied to your clipboard
using Com.Adobe.Marketing.Mobile;
  1. Register the Lifecycle extension:
Copied to your clipboard
public class TargetApp extends Application {
@Override
public void onCreate() {
super.onCreate();
MobileCore.setApplication(this);
try {
Lifecycle.registerExtension();
// register other extensions
MobileCore.start(new AdobeCallback () {
@Override
public void call(Object o) {
MobileCore.configureWithAppID("yourAppId");
}
});
} catch (Exception e) {
//Log the exception
}
}
}
  1. In the onResume function, start the lifecycle data collection:
Copied to your clipboard
@Override
public void onResume() {
MobileCore.setApplication(getApplication());
MobileCore.lifecycleStart(null);
}

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 setApplicationin each of your activities.

  1. In the onPause function, pause the lifecycle data collection:
Copied to your clipboard
@Override
public void onPause() {
MobileCore.lifecyclePause();
}

To ensure accurate session and crash reporting, this call must be added to every activity.

Swift

  1. Register the Lifecycle extension with the SDK Core by adding the following to your app's application:didFinishLaunchingWithOptions: delegate method:
Copied to your clipboard
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// register the lifecycle extension
ACPLifecycle.registerExtension();
}
  1. 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, then lifecycleStart: should only be called when the application state is not equal to UIApplicationStateBackground.

Copied to your clipboard
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// register the lifecycle extension
ACPLifecycle.registerExtension();
let appState = application.applicationState;
ACPCore.start {
// only start lifecycle if the application is not in the background
if appState != .background {
ACPCore.lifecycleStart(nil)
}
}
}
  1. When launched, if your app 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
func applicationWillEnterForeground(_ application: UIApplication) {
ACPCore.lifecycleStart(nil)
}

In iOS 13 and later, for a scene-based application, use the UISceneDelegate's sceneWillEnterForeground method as follows:

Copied to your clipboard
func sceneWillEnterForeground(_ scene: UIScene) {
ACPCore.lifecycleStart(nil)
}

For more information on handling foregrounding applications with Scenes, refer to Apple's documentation here

  1. When the app enters the background, pause Lifecycle data collection from your app's applicationDidEnterBackground: delegate method:
Copied to your clipboard
func applicationDidEnterBackground(_ application: UIApplication) {
ACPCore.lifecyclePause()
}

In iOS 13 and later, for a scene-based application, use the UISceneDelegate's sceneDidEnterBackground method as follows:

Copied to your clipboard
func sceneDidEnterBackground(_ scene: UIScene) {
ACPCore.lifecyclePause()
}

For more information on handling backgrounding applications with Scenes, refer to Apple's documentation here

Objective-C

  1. Register the Lifecycle extension with the SDK Core by adding the following to your app's application:didFinishLaunchingWithOptions: delegate method:
Copied to your clipboard
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// register the lifecycle extension
[ACPLifecycle registerExtension];
return YES;
}
  1. 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, then 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 {
// register the lifecycle extension
[ACPLifecycle registerExtension];
const UIApplicationState appState = application.applicationState;
[ACPCore start:^{
// only start lifecycle if the application is not in the background
if (appState != UIApplicationStateBackground) {
[ACPCore lifecycleStart:nil];
}
}];
}
  1. When launched, if your app 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 {
[ACPCore lifecycleStart:nil];
}

In iOS 13 and later, for a scene-based application, use the UISceneDelegate's sceneWillEnterForeground method as follows:

Copied to your clipboard
- (void) sceneWillEnterForeground:(UIScene *)scene {
[ACPCore lifecycleStart:nil];
}

For more information on handling foregrounding applications with Scenes, refer to Apple's documentation here

  1. 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 {
[ACPCore lifecyclePause];
}

In iOS 13 and later, for a scene-based application, use the UISceneDelegate's sceneDidEnterBackground method as follows:

Copied to your clipboard
- (void) sceneDidEnterBackground:(UIScene *)scene {
[ACPCore lifecyclePause];
}

For more information on handling backgrounding applications with Scenes, refer to Apple's documentation here

Registering the extension with Core

When using React Native, registering Lifecycle with Mobile Core should be done in native code which is shown under the Android and iOS (ACP 2.x) tabs.

When using Cordova, registering Lifecycle with Mobile Core must be done in native code which is shown under the Android and iOS tabs.

Starting and Pausing a lifecycle event

Add the OnApplicationPause in the MainScript with the following code:

Copied to your clipboard
private void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus)
{
ACPCore.LifecyclePause();
}
else
{
var cdata = new Dictionary<string, string>();
cdata.Add("launch.data", "added");
ACPCore.LifecycleStart(cdata);
}
}

iOS

  1. Register the Lifecycle extension with the SDK Core by adding the following to your app's FinishedLaunching: delegate method:
Copied to your clipboard
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
ACPLifecycle.RegisterExtension();
return base.FinishedLaunching(app, options);
}
  1. Start Lifecycle data collection by calling LifecycleStart: from within the callback of the ACPCore::start: method in your app's FinishedLaunching: delegate method.

If your iOS application supports background capabilities, your FinishedLaunching: 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, then LifecycleStart: should only be called when the application state is not equal to UIApplicationState.Background.

Copied to your clipboard
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
ACPLifecycle.RegisterExtension();
// only start lifecycle if the application is not in the background
var appstate = app.ApplicationState;
if(appstate != UIApplicationState.Background)
{
ACPCore.LifecycleStart(null);
}
return base.FinishedLaunching(app, options);
}
  1. When launched, if your app is resuming from a backgrounded state, iOS might call your WillEnterForeground: delegate method. You also need to call LifecycleStart:, but this time you do not need all of the supporting code that you used in FinishedLaunching::
Copied to your clipboard
public override void WillEnterForeground(UIApplication uiApplication)
{
base.WillEnterForeground(uiApplication);
ACPCore.LifecycleStart(null);
}
  1. When the app enters the background, pause Lifecycle data collection from your app's DidEnterBackground: delegate method:
Copied to your clipboard
public override void DidEnterBackground(UIApplication uiApplication)
{
base.DidEnterBackground(uiApplication);
ACPCore.LifecyclePause();
}

Android

  1. Register the Lifecycle extension:
Copied to your clipboard
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
LoadApplication(new App());
ACPCore.Application = this.Application;
ACPLifecycle.RegisterExtension();
}
  1. In the onResume function, start the lifecycle data collection:
Copied to your clipboard
protected override void OnResume()
{
base.OnResume();
ACPCore.LifecycleStart(null);
}

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. You should set the application (ACPCore.Application = this.Application;) in each of your activities.

  1. In the onPause function, pause the lifecycle data collection:
Copied to your clipboard
protected override void OnPause()
{
base.OnPause();
ACPCore.LifecyclePause();
}

To ensure accurate session and crash reporting, this call must be added to every activity.

Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.