gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:06:11 26 4
gpt4 key购买 nike

我正在尝试向我的应用程序添加应用程序内购买功能,并且我想下载我在自己的服务器上托管的内容。 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.

这是协议(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 调用!

希望对您有所帮助!

关于ios - 如何将自托管内容与交易相关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29466777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com