Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get trialDuration of a product in RevenueCat?

I am trying to dynamically display the trial duration of my annual product. I have found trialDuration in RC framework but I don’t know to use it. I am getting products like this:

private var offering: Offering? = SubscriptionManager.shared.offerings?.current

and then need to access the trial duration to display it via a text label. something like getting localizedPriceString for product price.

Any help would be great!

like image 601
Mc.Lover Avatar asked Sep 07 '25 00:09

Mc.Lover


2 Answers

RevenueCat SDK v4.21.1+

The localizedPriceString property is available for convenience on each Package object, and on Apple's App Store, free trials are provided as an introductory offer.

You can also create your own trialDuration property using the underlying storeProduct of StoreKit.

let discount = package.storeProduct.introductoryDiscount

Here's an implementation that returns a custom string representation of the trial period:

var introOfferDuration: String? {
    guard let discount = package.storeProduct.introductoryDiscount else {
        return nil
    }
    return discount.price == 0
    ? "\(discount.subscriptionPeriod.periodTitle()) for free"
    : "\(package.localizedIntroductoryPriceString!) for \(discount.subscriptionPeriod.periodTitle())"
}

// MARK: - RevenueCat SDK

extension SubscriptionPeriod {
    var durationTitle: String {
        switch self.unit {
        case .day: return "day"
        case .week: return "week"
        case .month: return "month"
        case .year: return "year"
        }
    }
    
    func periodTitle() -> String {
        let periodString = "\(self.value) \(self.durationTitle)"
        let pluralized = self.value > 1 ?  periodString + "s" : periodString
        return unit == .week ? "\(self.value * 7) days" : pluralized
    }
}
like image 65
Ajith R Nayak Avatar answered Sep 09 '25 13:09

Ajith R Nayak


I'm using the sk1Discount which is part of the StoreProductDiscount type.

For example use the following code snippet:

if let introductoryDiscount = package.storeProduct.introductoryDiscount {
    if let offer = introductoryDiscount.sk1Discount,
       introductoryDiscount.paymentMode == .freeTrial {
        Text("\(offer.subscriptionPeriod.numberOfUnits) \(offer.subscriptionPeriod.numberOfUnits > 1 ? offer.subscriptionPeriod.unit.localizedPluralString : offer.subscriptionPeriod.unit.localizedString) for free!")
    }
}

Hope that will help you. Note: I experienced that the sk1Discount is sometime in debug mode empty, but I never experienced any problems in production.

like image 44
Jonas Deichelmann Avatar answered Sep 09 '25 13:09

Jonas Deichelmann