2016-11-15 118 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! 
          } 
         }];