iOS - Restoring auto renewable subscription

2020-06-03 04:47发布

I am implementing auto renewable subscription.In that i have got the below question

Can a user be able to restore the subscription content even after it is expired?

if so how can i validate them and let them to download

2条回答
爷的心禁止访问
2楼-- · 2020-06-03 05:16

Look at Restoring Auto-Renewable Subscriptions

Basically you need to call [[SKPaymentQueue defaultQueue] restoreCompletedTransactions] and you'll get back restored transactions. You should look at originalTransaction property. You'll know transaction date for each transaction(also for expired ones). Then you look at product identifier and your app should know what's the lenght of subscription for each product identifier. And since you know start date and lenght of each transaction you can calculate time periods during which subscription was valid. You validate them as any other transaction - send them to your server which will send then to http://buy.itunes.apple.com/verifyReceipt (change buy to sandbox for testing).

查看更多
Deceive 欺骗
3楼-- · 2020-06-03 05:39
-(void)restoreSubscription:(SKPaymentTransaction *)transaction {
NSUserDefaults *defaultData = [NSUserDefaults standardUserDefaults];
NSArray *productID = [defaultData objectForKey:@"productID"];
NSArray *subMonths = [defaultData objectForKey:@"SubMonth"];

NSLog(@"%@",productID.description);

NSLog(@"Array ==%@",objContantManeger.subscriptionMonth);
NSMutableArray *arrID = [[NSMutableArray alloc] initWithArray:objContantManeger.subscriptionProductID];

NSMutableArray *arrMonth = [[NSMutableArray alloc] initWithArray:subMonths];

NSDate *date;
NSDate *exDate;
for (int i =0;i<arrID.count;i++)
{
    NSString *idStr =[arrID objectAtIndex:i];
    NSString *monthStr = [arrMonth objectAtIndex:i];
    int addsubMonth = [monthStr intValue];

    NSLog(@"%i--%@",i,idStr);
    NSLog(@"%i-->%i",i,addsubMonth);

    NSLog(@"Transaction Date--%@",transaction.originalTransaction.payment.productIdentifier);

    if ([transaction.originalTransaction.payment.productIdentifier isEqualToString:idStr])
    {
        NSLog(@"Date..%@",transaction.originalTransaction.transactionDate);


        date = transaction.originalTransaction.transactionDate;

        NSString *dateStr = [date description];
        NSRange range;

        // year
        range.location = 0;
        range.length = 4;
        NSString *yearStr = [dateStr substringWithRange:range];
        int year = [yearStr intValue];
        NSLog(@"%i",year);

        // month
        range.location = 5;
        range.length = 2;
        NSString *monthStr = [dateStr substringWithRange:range];
        int month = [monthStr intValue];
        NSLog(@"%i",month);
        // day
        range.location = 8;
        range.length = 2;
        NSString *dayStr = [dateStr substringWithRange:range];
        int day = [dayStr intValue];
        NSLog(@"%i",month);

        NSCalendar *calendar = [NSCalendar currentCalendar];

        NSDateComponents *components = [[NSDateComponents alloc] init];
        NSLog(@"addsubMonth---)%i",addsubMonth);
        [components setYear:year];
        [components setMonth:month+addsubMonth];
        [components setDay:day]; 

        NSLog(@"Expire DATE-->%@",[calendar dateFromComponents:components]);

        exDate = [calendar dateFromComponents:components];

        NSUserDefaults *defult = [NSUserDefaults standardUserDefaults];
        [defult setObject:exDate forKey:@"subexpiredate"];
        [defult synchronize];


       // return;

    }
}

You can use this method validate to the subscription.because this is provide subscription start and expire date.

Tips--

Implement Checking Subscription, using application delegate method applicationDidEnterBackground.

查看更多
登录 后发表回答