iPhone - How to retrieve duration for auto-renewable subscription for in app purchases
Asked Answered
W

3

14

I'm looking at setting up In App Purchases for an iPhone app. I'm planning on using the new auto-renewable subscription type. However, I want to offer multiple durations for a particular subscription, but can't see how I can retrieve the duration from the SKProduct that is returned in the SKProductsResponse.products array.

The SKProduct object has price, localizedTitle and localizedDescription. However, if you set up a subscription family with multiple durations the title/description are set once for the family so you cannot include the duration, and the docs explicitly say don't include the duration in the title/description. However, can't see any other field where I can retrieve the duration for displaying in my custom in app store. Either I'm missing something or it isn't going to be available until 4.3?

Pointers greatly appreciated!

Wendelin answered 6/3, 2011 at 7:43 Comment(0)
S
11

You need to have some mapping product_id => length somewhere, either in your app or retrived from your app's backend.

School answered 27/3, 2011 at 9:21 Comment(1)
Yep this is effectively what I ended up doing.Wendelin
A
9

You can use a specific productIdentifier for each duration (in the code below the productIdentifier for a 1 month subscription is "com.domainname.myapp.sub1month" and for a 7 day duration it is "com.domainname.myapp.sub7day") and search for that in the paymentQueue:

-(void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
   for(SKPaymentTransaction *transaction in transactions){
     switch (transaction.transactionState){
        case SKPaymentTransactionStatePurchasing:
            break;
        case SKPaymentTransactionStatePurchased:
            if([transaction.payment.productIdentifier isEqualToString:@"com.domainname.myapp.sub1month"]{
                newSubscriptionEndDate=[transaction.transactionDate timeIntervalSinceReferenceDate]+3600*24*31;
            }
            if([transaction.payment.productIdentifier isEqualToString:@"com.domainname.myapp.sub7day"]  ){
                newSubscriptionEndDate=[transaction.transactionDate timeIntervalSinceReferenceDate]+3600*24*7;
            }
            [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
            break;
Amber answered 30/3, 2011 at 18:34 Comment(0)
F
6

iOS 11.2 brings the subscriptionDuration property to SKProduct. I don't think there is a fallback for older iOS though.

https://developer.apple.com/documentation/storekit/skproduct/2936884-subscriptionperiod

Fictionist answered 1/3, 2018 at 18:8 Comment(1)
Hooray... 11 years to figure out someone might want a way to access that information.... who would have thought? lolIiette

© 2022 - 2024 — McMap. All rights reserved.