我正在尝试向我的应用程序添加应用程序内购买功能,并且我想下载我在自己的服务器中托管的内容。 RMStore 提供了一个 API 来做到这一点,但是我不知道如何去做。
文档说:
- (void)downloadContentForTransaction:(SKPaymentTransaction*)transaction
success:(void (^)())successBlock
progress:(void (^)(float progress))progressBlock
failure:(void (^)(NSError *error))failureBlock;
这是协议(protocol)(来自 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
简单地说,下载与给定交易相关联的自托管内容。如何将自托管与交易相关联?
最佳答案
这是我所做的。显然,您需要在运行此方法的类中添加 RMStore.h
和协议(protocol) 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
调用!希望能帮助到你!