2016-11-15 116 views
1

我正在將應用程序從付費轉換爲免費,以及IAP下的一些前功能。因此,現有用戶需要擁有IAP的副本。爲了做到這一點,我使用Apple's Website的收據驗證碼。在這種情況下,我的目標不是實際驗證收據的合法性,而是取回用戶購買的版本號,以便我可以檢測它們是否爲付費用戶(感謝您對this question的建議)。如何在這種情況下測試收據驗證?

NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; 
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL]; 
if (!receipt) { NSLog(@"No receipt found"); return; } 

這是我用來獲取用戶收據的代碼。它與上述官方蘋果網站上的代碼幾乎相同。但是,我仍然想要測試它以及它後面的代碼,這會授予用戶他們的IAP。

但是,上述代碼會記錄「找不到收據」,如果我通過Xcode在我的iPhone上或通過TestFlight在我的iPhone上運行程序,則返回。我安裝了當前的App Store版本,然後嘗試了TestFlight,但它仍然給出了相同的無回執錯誤。

如何獲取測試收據的副本,此外,我將如何測試這種收據驗證的形式?

+0

據我所知,一個dev的建立將不會有收據,直到你在沙盤完成IAP。 – Paulw11

回答

2

SKReceiptRefreshRequest將提供一個假的收據,您將在蘋果的沙箱驗證服務器上進行驗證。調用SKReceiptRefreshRequest是我錯過的元素。

SKReceiptRefreshRequest *receiptRequest = [[SKReceiptRefreshRequest alloc] initWithReceiptProperties:nil]; 
receiptRequest.delegate = self; 
[receiptRequest start]; 

-

- (void)requestDidFinish:(SKRequest *)request { 
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; 
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL]; 
if (!receipt) { NSLog(@"No receipt found"); return; } 
// Create the JSON object that describes the request 
NSError *error; 
NSDictionary *requestContents = @{ 
            @"receipt-data": [receipt base64EncodedStringWithOptions:0] 
            }; 
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents 
                 options:0 
                 error:&error]; 

if (!requestData) { NSLog(@"No request data found"); return; } 

// Create a POST request with the receipt data. 
NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"]; 
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL]; 
[storeRequest setHTTPMethod:@"POST"]; 
[storeRequest setHTTPBody:requestData]; 
// Make a connection to the iTunes Store on a background queue. 
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue 
         completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
          if (connectionError) { 
           NSLog(@"Connection error"); 
           return; 
          } else { 
           NSError *error; 
           NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; 
           if (!jsonResponse) { return; } 
           NSLog(@"JsonResponce: %@",jsonResponse); 
           NSString *version = jsonResponse[@"receipt"][@"original_application_version"]; 
           //found version number! Do whatever with it! 
          } 
         }];