• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 22:48:17 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在努力将一款应用从付费转换为免费,其中包含 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,它仍然给出了同样的 no-receipt 错误。

如何获取收据副本进行测试,此外,我将如何测试这种形式的收据验证?



Best Answer-推荐答案


SKReceiptRefreshRequest 将提供一个假收据,您可以在苹果的沙盒验证服务器上对其进行验证。调用 SKReceiptRefreshRequest 是我缺少的元素。

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

-

- (void)requestDidFinishSKRequest *)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"OST"];
[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!
                           }
                       }];

关于ios - 在这种情况下如何测试收据验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40600584/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap