如何将自托管内容与交易相关联?

How to associate self hosted content with transaction?

我正在尝试将应用内购买功能添加到我的应用程序中,并且我想下载我在自己的服务器上托管的内容。 RMStore 提供了一个 API 来执行此操作,但是我不知道该怎么做。

文档说:

RMStore delegates the downloading of self-hosted content via the optional contentDownloader delegate. You can provide your own implementation using the RMStoreContentDownloader protocol:

- (void)downloadContentForTransaction:(SKPaymentTransaction*)transaction
                              success:(void (^)())successBlock
                             progress:(void (^)(float progress))progressBlock
                              failure:(void (^)(NSError *error))failureBlock;

Call successBlock if the download is successful, failureBlock if it isn't and progressBlock to notify the download progress. RMStore will consider that a transaction has finished or failed only after the content downloader delegate has successfully or unsuccessfully downloaded its content.

这是协议(来自 RMStore.h):

@protocol RMStoreContentDownloader <NSObject>

/**
 Downloads the self-hosted content associated to the given transaction and calls the given success or failure block accordingly. Can also call the given progress block to notify progress.
 @param transaction The transaction whose associated content will be downloaded.
 @param successBlock Called if the download was successful. Must be called in the main queue.
 @param progressBlock Called to notify progress. Provides a number between 0.0 and 1.0, inclusive, where 0.0 means no data has been downloaded and 1.0 means all the data has been downloaded. Must be called in the main queue.
 @param failureBlock Called if the download failed. Must be called in the main queue.
 @discussion Hosted content from Apple’s server (@c SKDownload) is handled automatically by RMStore.
 */
- (void)downloadContentForTransaction:(SKPaymentTransaction*)transaction
                              success:(void (^)())successBlock
                             progress:(void (^)(float progress))progressBlock
                              failure:(void (^)(NSError *error))failureBlock;

@end

简而言之,下载与给定交易关联的自托管内容。如何将自托管与交易相关联?

您尝试通过应用内购买提供的内容对于每笔交易都是唯一的吗?如果它对于每笔交易都是唯一的,您应该将交易 ID 传递给您的服务器并下载仅为该交易 ID 生成的内容。否则,对于每个事务下载内容而不传递事务 id。对于这两种情况,您都应该在下载过程结束时调用 successBlock 或 failureBlock。或者,您可以在每次要更新进度时调用 progressBlock。

这是我所做的。显然,您需要在 运行 此方法所在的 class 中添加 RMStore.h 和协议 RMStoreContentDownloader。 它有效,虽然我不明白它是如何管理的progressBlock(也许我的下载太短了?)...

- (void)downloadContentForTransaction:(SKPaymentTransaction*)transaction
                          success:(void (^)())successBlock
                         progress:(void (^)(float progress))progressBlock
                          failure:(void (^)(NSError *error))failureBlock
{
    //the product purchased
    NSString *productID = transaction.payment.productIdentifier;


    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    //HERE IS WHERE TO INSERT THE URL
    NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    if (error == nil)
        NSLog(@"File downloaded to: %@", filePath);
        successBlock();
    else
        NSLog(@"Error in download: %@", error.localizedDescription);
        failureBlock();
    }];
    [downloadTask resume];
    [manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session,    NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite)
    {
        float percentDone = (((float)((int)totalBytesWritten) / (float)((int)totalBytesExpectedToWrite))*100);
        progressBlock(percentDone);
    }];

}

然后该方法将在适当的时候被RMStore调用!

希望对您有所帮助!