我正在尝试将应用内购买功能添加到我的应用中,并且我想下载我在自己的服务器中托管的内容。 RMStore提供了一个API来执行此操作,但我无法弄清楚如何执行此操作。
文档说:
RMStore委托通过以下方式下载自托管内容 可选的
contentDownloader
代表。你可以提供自己的 使用RMStoreContentDownloader
协议实现:
- (void)downloadContentForTransaction:(SKPaymentTransaction*)transaction
success:(void (^)())successBlock
progress:(void (^)(float progress))progressBlock
failure:(void (^)(NSError *error))failureBlock;
如果下载成功,请致电
successBlock
,如果是failureBlock
则致电{1}} isn&t;和progressBlock
通知下载进度。 RMStore会 考虑到交易在完成或失败之后才会完成 内容下载程序委托已成功或失败 下载了它的内容。
以下是协议(来自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
简单地说,下载与给定交易相关联的自托管内容。如何将自托管与交易相关联?
答案 0 :(得分: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
在适当的时候调用该方法!
希望它有所帮助!
答案 1 :(得分:-1)
您尝试通过应用内购买提供的内容是否对每笔交易都是唯一的?如果它对每个事务都是唯一的,则应将事务ID传递给服务器并下载仅为此事务ID生成的内容。否则,为每个事务下载内容而不传递事务id。对于这两种情况,您应该在下载过程结束时调用successBlock或failureBlock。 (可选)您可以在每次要更新进度时调用progressBlock。