4

I'm trying to add in-app purchase feature to my application and I want to download contents that I host in my own server. RMStore provides an API to do this, however I couldn't figure out how to do it.

Documentation says:

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.

And here is the protocol (from 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

Simply it says, downloads the self-hosted content associated to the given transaction. How do I associate the self-hosted to transaction?

4

2 回答 2

2

这就是我所做的。显然,您需要在运行此方法的类中添加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然后该方法将在适当的时候被调用!

希望能帮助到你!

于 2015-12-18T10:31:07.747 回答
-1

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

于 2015-04-06T13:31:27.470 回答