2015-04-06 56 views
4

我正在嘗試將應用內購買功能添加到我的應用程序中,並且我想下載託管在我自己的服務器中的內容。 RMStore提供了一個API來做到這一點,但我無法弄清楚如何做到這一點。如何將自我託管的內容與交易相關聯?

文件說:

RMStore代表通過 可選contentDownloader代表的自託管內容的下載。您可以使用RMStoreContentDownloader協議提供自己的 實現:

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

呼叫successBlock如果下載成功,failureBlock如果 是不是和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 

只需將它說,下載相關的特定交易自託管的內容。我如何將自託管與交易相關聯?

回答

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在適當的時候被調用!

希望它有幫助!

+0

您的答案不提供將自託管內容與交易關聯的任何解決方案。 – guneykayim 2015-12-18 11:08:44

+0

我只是更新我的答案:'productIdentifier'在'SKPaymentTransaction'裏面! – Zanzi 2015-12-18 11:48:11

+0

如何啓動下載?例如,這是我的下載鏈接http://stackoverflow.com/users/flair/1249328.png在哪裏以及如何開始下載過程並將其與交易相關聯? – guneykayim 2015-12-18 11:53:39

-1

您嘗試通過應用程序內購買爲每筆交易提供的內容是否爲您提供的內容?如果它對於每個事務都是唯一的,那麼您應該將事務標識傳遞給您的服務器並下載僅爲此事務標識生成的內容。否則,對於每個事務下載內容而不傳遞事務ID。對於這兩種情況,應在下載過程結束時調用successBlock或failureBlock。或者,您可以在每次要更新進度時調用progressBlock。

+0

內容對於每個應用內商品都是唯一的,而不是每個交易。我的問題是我不確定如何啓動下載過程並將其與'SKPaymentTransaction'關聯。我應該在哪一步發送交易ID?爲什麼我應該發送交易ID?我應該如何發送交易ID(通過網絡服務)? – guneykayim 2015-04-06 14:05:43

+0

如果您有適用於應用內產品的內容,則可以將您的內容放入簡單的虛擬主機並根據應用內購買標識命名您的文件。當用戶購買產品時,SKPaymentTransaction具有標識符。在實現downloadContentForTransaction方法時,您將擁有該對象。您也將在該方法內開始下載過程。您可以使用第三方http庫或簡單NSUrlConnection來下載您的文件。 – furkan3ayraktar 2015-04-06 14:14:57