Search code examples
iosswiftin-app-purchasesubscription

How to make IAP purchase request with free trial period?


I am implementing subscription model to my app and I prepared everything. I created a Monthly Subscription product and a Yearly. Also I add a promotional offer for Monthly product. The promotional offer is 1 week free trial.

When I try to purchase the monthly product, It is starting monthly subscription immediately. How can I provide start with trial offer first?

let payment = SKPayment(product: MonthlyProduct)
SKPaymentQueue.default().add(payment)

Also When I was fetching available products, Only came 2 product (Monthly and Yearly). I know free trial offer under the Monthly product but I couldn't find how to purchase free trial offer.


Solution

  • I found How can I do payment request subscription product with trial period. I have to set payment's paymentDiscount with a SKPaymentDiscount object.

    let payment = SKPayment(product: product)
    payment.paymentDiscount = SKPaymentDiscount(identifier: <String>, keyIdentifier: <String>, nonce: <UUID>, signature: <String>, timestamp: <NSNumber>)
    SKPaymentQueue.default().add(payment)
    

    Before we can apply the offer we need to convert our SKProductDiscount into an SKPaymentDiscount. The init method for SKPaymentDiscount provides some clue to what we’ll need to achieve that:

    • identifier — The identifier of the subscription offer
    • keyIdentifier — The identifier of the subscription key used to sign the offer
    • nonce — A throwaway value generated along with the signature
    • signature — The signature itself
    • timestamp — The timestamp when the signature was generated.

    You can find full process this tutorial