【问题标题】:Get subscription expiration date for in-app purchase- starting with appStoreReceiptURL获取应用内购买的订阅到期日期 - 从 appStoreReceiptURL 开始
【发布时间】:2013-11-17 09:27:37
【问题描述】:

我有一个自动续订订阅的应用内购买。从完成的应用内购买中,我需要能够检索订阅到期日期,以便我知道订阅何时不再有效。我的理解是,这意味着(现在从 iOS 7 开始)从 appStoreReceiptURL 开始,获取其背后的数据,然后以某种方式从那里我可以访问有关应用内购买的元素。

但是从那里开始变得模糊不清,我在网上找到的所有代码示例似乎都不起作用。其中很多对于 iOS 7 来说已经过时了。它们中的大多数根本不处理订阅问题。我真正需要的是一些从 appStoreReceiptURL 到订阅到期日期直线的示例代码,中间的代码行数最少。由于某种原因,这似乎非常困难,但我不明白为什么。人们经常将收据验证纳入其中,这似乎是个好主意,但它也增加了另一个巨大的复杂性,使我的主要目标变得模糊,而且这个收据验证代码似乎总是不起作用。我似乎总是碰壁的地方是我采用 NSData 和 base64 对其进行编码的地方。看起来应该是这样的:

NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSLog(@"receiptURL:%@", receiptURL);

NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
NSLog(@"receiptData:%@", receiptData);

NSString *receiptString = [self base64forData:receiptData];
NSLog(@"receiptString:%@", receiptString);

base64forData 方法如下所示:

// from http://stackoverflow.com/questions/2197362/converting-nsdata-to-base64
+ (NSString*)base64forData:(NSData*)theData {
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];
    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;
    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }
        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }
    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

在输出中,我得到了一个有效的receiptURL,但前提是我执行了应用程序委托中的代码。如果我尝试在像我的商店观察者这样的子类中执行它,它总是返回 nil。我暂时忽略了这一点,只是在应用程序委托中执行它。然后,如果receiptURL 有效,那么我得到一个有效的receiptData 块,然后receiptString 为零。所以看起来好像 base64forData 方法不起作用,或者整个方法是错误的?

从 iOS 7 中未弃用的已完成 SKPaymentTransaction 获取订阅到期日期的最简单方法是什么?

【问题讨论】:

  • 嗨约翰,你解决了这个问题吗?我有同样的问题,搜索了很长时间,但一直无法弄清楚
  • 使用苹果 iTunes 收据验证服务 - 请参阅
  • 很高兴看到这些日志行的输出与我自己的数据进行比较,以了解我是否有正确的数据开始。

标签: ios objective-c in-app-purchase


【解决方案1】:

我们使用 Apple 的应用商店收据验证服务。 Tom Kincaid 对这个问题的回答中有很好的示例代码:-

StackOverflow app store receipt question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-03
    • 2016-07-08
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多