iOS in-app purchase subscription get free trial period length from SKProduct

mjpablo23 picture mjpablo23 · Aug 22, 2017 · Viewed 8.6k times · Source

I am working on in-app purchases with subscriptions. In swift, you can get price and price locale from the SKProduct like so:

weeklyProduct.price.doubleValue  
weeklyProduct.priceLocale.currencySymbol

where weeklyProduct is a SKProduct.

Is it possible to get the free trial length? For example, I specified a two week free trial for the product. can I get this from the SKProduct?

Answer

Binshakerr picture Binshakerr · May 30, 2018

You can get it, but as mentioned above it works only starting from iOS 11.2, for other versions you'll have to get it from your server via API.

Here is an example code that I've used:

if #available(iOS 11.2, *) {
  if let period = prod.introductoryPrice?.subscriptionPeriod {
     print("Start your \(period.numberOfUnits) \(unitName(unitRawValue: period.unit.rawValue)) free trial")
  }
} else {
  // Fallback on earlier versions
  // Get it from your server via API
}

func unitName(unitRawValue:UInt) -> String {
    switch unitRawValue {
    case 0: return "days"
    case 1: return "weeks"
    case 2: return "months"
    case 3: return "years"
    default: return ""
    }
}