How to link self-hosted content to a transaction?

I am trying to add an app purchase feature to my app, and I want to download the content that I host on my own server. RMStore provides an API for this, however I could not figure out how to do this.

The documentation says:

RMStore delegates the loading of its own content through an optional delegate contentDownloader. You can provide your own using the protocol RMStoreContentDownloader:

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

Call successBlockif the download is successful, failureBlockif it is not and progressBlocknotify the download progress. RMStore will believe that the transaction completed or failed only after the delegate of the content downloader successfully or unsuccessfully downloaded its contents.

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

He just says loading the self-serving content related to this transaction. How to associate a transaction with an independent organization?

+4
source share
2 answers

Here is what I did. Obviously, you need to add the RMStore.hprotocol RMStoreContentDownloaderto the class in which you run this method. It works, although I did not understand how it is controlled progressBlock(maybe my load is too short?) ...

- (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);
    }];

}

Then the method will be called RMStoreat the right time!

Hope this helps!

+2
source

, , ? , , . . successBlock failBlock . progressBlock , .

-1

All Articles