在这种情况下如何测试收据验证?

How to test receipt validation in this case?

我正在努力将一个应用程序从付费转换为免费,其中一些以前的功能在 IAP 下。因此,现有用户需要一份 IAP。为此,我使用 Apple's Website. My goal in this case is not to actually validate the receipt for legitimacy, but instead to get back the version number that the user bought so I can detect if they are a paid user (thank you to this question 的收据验证码作为建议)。

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

这是我用来获取用户收据的代码。它与上述苹果官方网站上的代码几乎相同。但是,我仍然想测试它和它后面的代码,它授予用户他们的 IAP。

但是,如果我 运行 模拟器中的程序,上面的代码将记录 "No receipt found" 和 return,通过 Xcode 在我的 iPhone 上,或者通过 TestFlight 在我的 iPhone 上。我安装了当前的 App Store 版本,然后尝试了 TestFlight,它仍然给出相同的无回执错误。

如何获得用于测试的收据副本,此外,我将如何测试这种形式的收据验证?

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!
                           }
                       }];