robotmedia / RMStore

A lightweight iOS library for In-App Purchases
Apache License 2.0
2.43k stars 450 forks source link

Selective restoration of in-app, Apple hosted, downloads. #128

Open Seoras opened 9 years ago

Seoras commented 9 years ago

If I'm reading the API correctly then I'm assuming restoreTransactionsOnSuccess: downloads all available purchased content before calling any delegate or block. With the exception being storeDownloadUpdated:

Is there anyway to discover what content is available for restore and selectively restoring?

Horse888 commented 9 years ago

I reach the same issue. I find the code in RMStore.m line 623: [queue startDownloads:downloads];

That means after restore finished, all the content will start download again. I think adding a property to indicate which product IDs no need to be re-downloaded will fix.

I changed the code below temporary: I add a NSArray *_restoreSkipReDownloadProductIDs;

- (void)didDownloadSelfHostedContentForTransaction:(SKPaymentTransaction *)transaction queue:(SKPaymentQueue*)queue
{
    NSArray *downloads = transaction.downloads;

    NSMutableArray *realyDownloads = [@[] mutableCopy];

    if (downloads.count > 0)
    {
        for (SKDownload *download in downloads) {
            if ([_restoreSkipReDownloadProductIDs containsObject: download.contentIdentifier]) {
                [self finishTransaction:transaction queue:queue];
            } else {
                [realyDownloads addObject: download];
            }
        }

        RMStoreLog(@"starting downloads for product %@ started", realyDownloads);

        [queue startDownloads:realyDownloads];
    } else {
        [self finishTransaction:transaction queue:queue];
    }
}
Horse888 commented 9 years ago

Sure, I changed a little to the restore function:

- (void)restoreTransactionsOnSuccess:(void (^)(NSArray *transactions))successBlock
                             failure:(void (^)(NSError *error))failureBlock
{
    [self restoreTransactionsBySkipReDownloadProductID: nil
                                           OnSuccess: successBlock
                                             failure: failureBlock];
}

- (void)restoreTransactionsBySkipReDownloadProductID:(NSArray *)skipReDownloadProductIDs
                                         OnSuccess:(void (^)(NSArray *transactions))successBlock
                                           failure:(void (^)(NSError *error))failureBlock;
{
    _restoreSkipReDownloadProductIDs = skipReDownloadProductIDs;
    _restoredCompletedTransactionsFinished = NO;
    _pendingRestoredTransactionsCount = 0;
    _restoredTransactions = [NSMutableArray array];
    _restoreTransactionsSuccessBlock = successBlock;
    _restoreTransactionsFailureBlock = failureBlock;
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
Horse888 commented 8 years ago

@hpique Will you update the code?