|
| 1 | +# Lottie Animations — Universal Concept |
| 2 | + |
| 3 | +Applies to: **iOS and Android native paywall rendering**. React Native, Flutter and Cordova apps must configure the underlying iOS / Android host projects if their Purchasely Screens use Lottie blocks. |
| 4 | + |
| 5 | +Purchasely supports Lottie animations in Screens, but Lottie is a **weak dependency**: the SDK does not bundle Airbnb Lottie to keep the SDK lightweight. If a Screen contains a Lottie block, the app must provide both: |
| 6 | + |
| 7 | +1. the Lottie dependency in the app, and |
| 8 | +2. a small bridge/interface with the stable methods Purchasely calls at runtime. |
| 9 | + |
| 10 | +Official doc: <https://docs.purchasely.com/docs/lottie-animations> |
| 11 | + |
| 12 | +## Console availability |
| 13 | + |
| 14 | +The Lottie option is added template by template in the Purchasely Console. If a team needs Lottie in a template where the option is not visible, contact Purchasely Support rather than trying to fix it in app code. |
| 15 | + |
| 16 | +## Runtime behavior |
| 17 | + |
| 18 | +At runtime the SDK detects Lottie support and inserts the animation view into the paywall. The SDK can configure: |
| 19 | + |
| 20 | +- animation URL, |
| 21 | +- repeat vs play once, |
| 22 | +- aspect behavior (`fill` / `fit`). |
| 23 | + |
| 24 | +If the bridge is missing, the app can display the paywall but the Lottie animation will not render correctly. |
| 25 | + |
| 26 | +## iOS setup |
| 27 | + |
| 28 | +Add Airbnb Lottie to the app if it is not already present, for example via CocoaPods or SPM (`lottie-ios`, module `Lottie`). Then add a Swift class named `PLYLottieBridge` and expose it to Objective-C with `@objc(PLYLottieBridge)`. |
| 29 | + |
| 30 | +```swift |
| 31 | +import UIKit |
| 32 | +import Lottie |
| 33 | + |
| 34 | +@objc(PLYLottieBridge) |
| 35 | +class PLYLottieBridge: NSObject { |
| 36 | + var animationView: LottieAnimationView? |
| 37 | + |
| 38 | + @objc class func bridge(with animationURL: URL) -> PLYLottieBridge? { |
| 39 | + let result = PLYLottieBridge() |
| 40 | + result.animationView = LottieAnimationView(url: animationURL, closure: { [weak result] _ in |
| 41 | + result?.animationView?.play() |
| 42 | + }, animationCache: nil) |
| 43 | + result.animationView?.loopMode = .loop |
| 44 | + return result |
| 45 | + } |
| 46 | + |
| 47 | + @objc func view() -> UIView? { |
| 48 | + return animationView |
| 49 | + } |
| 50 | + |
| 51 | + @objc func loop(_ loop: Bool) { |
| 52 | + animationView?.loopMode = loop ? .loop : .playOnce |
| 53 | + } |
| 54 | + |
| 55 | + @objc func fill(_ fill: Bool) { |
| 56 | + animationView?.contentMode = fill ? .scaleAspectFill : .scaleAspectFit |
| 57 | + } |
| 58 | + |
| 59 | + @objc func play() { |
| 60 | + animationView?.play { _ in } |
| 61 | + } |
| 62 | + |
| 63 | + @objc func pause() { |
| 64 | + animationView?.pause() |
| 65 | + } |
| 66 | + |
| 67 | + @objc func stop() { |
| 68 | + animationView?.stop() |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Notes: |
| 74 | + |
| 75 | +- Keep the Objective-C name exactly `PLYLottieBridge`; the SDK looks for that stable bridge. |
| 76 | +- If the app already has Lottie, reuse its pinned version unless there is a known incompatibility. |
| 77 | + |
| 78 | +## Android setup |
| 79 | + |
| 80 | +Android support requires Purchasely SDK **3.6.0+**; all current 5.x SDKs qualify. Add Airbnb Lottie to the app if needed: |
| 81 | + |
| 82 | +```kotlin |
| 83 | +dependencies { |
| 84 | + implementation("com.airbnb.android:lottie:<app-approved-version>") |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +Create a view implementing `PLYLottieInterface`: |
| 89 | + |
| 90 | +```kotlin |
| 91 | +import android.animation.ValueAnimator |
| 92 | +import android.content.Context |
| 93 | +import android.util.Log |
| 94 | +import android.widget.ImageView |
| 95 | +import androidx.annotation.Keep |
| 96 | +import com.airbnb.lottie.LottieAnimationView |
| 97 | +import com.airbnb.lottie.LottieDrawable |
| 98 | +import io.purchasely.views.presentation.interfaces.PLYLottieInterface |
| 99 | + |
| 100 | +private const val TAG = "PLYLottie" |
| 101 | + |
| 102 | +@Keep |
| 103 | +class AnimationView(context: Context) : LottieAnimationView(context), PLYLottieInterface { |
| 104 | + override fun setup(url: String, repeat: Boolean, scaleType: ImageView.ScaleType) { |
| 105 | + setAnimationFromUrl(url) |
| 106 | + enableMergePathsForKitKatAndAbove(true) |
| 107 | + repeatMode = LottieDrawable.RESTART |
| 108 | + repeatCount = if (repeat) ValueAnimator.INFINITE else 0 |
| 109 | + this.scaleType = scaleType |
| 110 | + setFailureListener { error -> |
| 111 | + Log.e(TAG, "Unable to load Lottie animation", error) |
| 112 | + } |
| 113 | + play() |
| 114 | + } |
| 115 | + |
| 116 | + override fun play() { |
| 117 | + playAnimation() |
| 118 | + } |
| 119 | + |
| 120 | + override fun stop() { |
| 121 | + cancelAnimation() |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +Register the factory during app initialization, before displaying paywalls: |
| 127 | + |
| 128 | +```kotlin |
| 129 | +Purchasely.lottieView = { context -> AnimationView(context) } |
| 130 | +``` |
| 131 | + |
| 132 | +## Cross-platform apps |
| 133 | + |
| 134 | +React Native, Flutter and Cordova paywalls are rendered by the native iOS / Android Purchasely SDKs. If a cross-platform app uses Screens with Lottie: |
| 135 | + |
| 136 | +- add the iOS `PLYLottieBridge` + Lottie dependency in the iOS host project; |
| 137 | +- add the Android `PLYLottieInterface` implementation + `Purchasely.lottieView` factory in the Android host project; |
| 138 | +- keep the cross-platform Purchasely packages aligned with `../sdk-versions.md` as usual. |
| 139 | + |
| 140 | +## Troubleshooting |
| 141 | + |
| 142 | +| Symptom | Check | |
| 143 | +|---------|-------| |
| 144 | +| Lottie area is blank / static | Native bridge missing or not loaded before the paywall display. | |
| 145 | +| Crash or error while loading animation | Add Android `setFailureListener`, check device logs, verify the remote JSON URL is reachable. | |
| 146 | +| Animation works in preview but not in app | Confirm the app includes Lottie and the bridge on the target platform. | |
| 147 | +| Animation renders badly or not at all | Test the file in LottieFiles Preview and check for unsupported expressions/effects. | |
| 148 | +| Memory / rendering issues | Keep Lottie JSON under **2 MB**; larger files can fail or cause memory pressure. | |
| 149 | +| Lottie option missing in Console | Feature is enabled template-by-template; ask Purchasely Support for that template. | |
| 150 | + |
| 151 | +## See also |
| 152 | + |
| 153 | +- [screen-issue-report.md](../troubleshooting/screen-issue-report.md) — package a reproducible Composer issue for Support |
| 154 | +- [debug-mode.md](../troubleshooting/debug-mode.md) — preview draft Screens on device |
| 155 | +- [presentation-types.md](presentation-types.md) — guard `NORMAL` / `FALLBACK` / `DEACTIVATED` before display |
0 commit comments