已弃用的TransactionReceipt

30

我正在使用这段代码进行应用内购买,它来自RaywernderLich的教程。

// Encode the receiptData for the itms receipt verification POST request.
NSString *jsonObjectString = [self encodeBase64:(uint8_t *)transaction.transactionReceipt.bytes
                                         length:transaction.transactionReceipt.length];

现在Xcode提示:

'transactionReceipt' 已弃用:自iOS 7.0起首次弃用

如何修复?

3个回答

23

关于弃用

由于这个问题技术上在询问如何处理废弃属性,可以合理地假设 OP 仍在部署 iOS 7 以下的版本。因此,您需要检查是否有新的 API 可用,而不是盲目调用它:

Objective-C

编辑 正如评论中指出的那样,您不能在先前的 iOS 版本上使用 respondsToSelector 来调用 NSBundle,因为该 API 是私有的。

NSData *receiptData;
if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_7_0) {
    receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
} else {
    receiptData = transaction.transactionReceipt;
}
//now you can convert receiptData into string using whichever encoding:)

Swift

由于Swift只能在iOS 7及以上版本部署,因此我们可以安全地使用appStoreReceiptURL

if let receiptData = NSData(contentsOfURL: NSBundle.mainBundle().appStoreReceiptURL!) {
    //we have a receipt
}

关于验证收据

新版API中,收据现在包含用户执行的所有交易列表。 文档清楚地概述了收据的外观:

receipt outline

这意味着如果您真的非常想要,可以迭代遍历收据中包含的所有项目以根据每个交易进行验证。

有关收据验证的更多信息,您可以阅读objc.io


4
在这种特定情况下,你不应该使用 respondsToSelector。苹果明确表示它在这种情况下不起作用。你应该比较实际的版本号。RTFM ;) https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/#//apple_ref/occ/instp/NSBundle/appStoreReceiptURL - Maciej Swic
2
@MaciejSwic 谢谢您!我不知道那个,我会更新。 - Daniel Galasko
这只是部分正确,以前苹果不鼓励本地验证,格式是“封闭”的,随时可能更改。但也许一直都是这样,只是最近才被记录下来,我不知道详情,只是想提出这个问题。 - Maciej Swic
@DanielGalasko iOS 版本有误吗?我认为你应该使用 if(NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_7_0)。 - Francesco Puglisi
@singingAtom 你说得对!谢谢!我刚更新了我的答案。 - Daniel Galasko
显示剩余5条评论

17

替换为以下内容:

[NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];

NSData转换为NSString,然后......


阅读此内容:https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html#//apple_ref/doc/uid/TP40010573-CH104-SW1 - Nikos M.
你能在应用内购买中使用这个吗? - locoboy
1
这是不正确的,也不适用于应用内购买,而这正是本教程所讲述的内容。 - Maciej Swic
实际上它确实包含IAP收据:https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateLocally.html#//apple_ref/doc/uid/TP40010573-CH1-SW3 - Rick

10
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
if(!receipt) {
 /* No local receipt -- handle the error. */ 
}
NSString *jsonObjectString = [receipt base64EncodedString];

这仅验证应用程序,而不验证任何交易收据中的应用内购买。 - Maciej Swic
收据中包含用户进行的所有应用内购买的数据。 - Radu Diță

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接