不推荐使用的TransactionReceipt

Ali*_*yan 28 iphone objective-c in-app-purchase ios7

我正在使用此代码进行应用内购买,从RaywernderLich的教程中获取.

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

现在Xcode说

不推荐使用'transactionReceipt':首先在iOS 7.0中弃用

怎么解决?

Dan*_*sko 20

关于弃用

由于这个问题在技术上询问人们应如何解决已弃用的属性,因此可以假设OP仍在部署在小于7的iOS版本上.因此,您需要检查新API的可用性,而不是盲目地调用它:

Objective-C的

编辑 正如评论中指出的那样,您无法在NSBundle上使用respondsToSelector,因为该API在之前的iOS版本中是私有的

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:)
Run Code Online (Sandbox Code Playgroud)

迅速

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

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

关于收据验证

收据现在包含的新API包含用户执行的所有交易的列表.该文件清楚地概述了收据的样子:

收据大纲

这意味着如果您真的真的想要,您可以遍历收据中包含的所有项目以验证每个事务.

有关收据验证的更多信息,请阅读obc.io

  • 在这种特殊情况下,不应该使用respondsToSelector.Apple明确指出它在这种情况下不起作用.您应该比较实际版本号.RTFM;)https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSBundle_Class/#//apple_ref/occ/instp/NSBundle/appStoreReceiptURL (4认同)
  • @MaciejSwic谢谢先生!不知道,会更新 (2认同)

Nik*_* M. 16

替换为:

[NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
Run Code Online (Sandbox Code Playgroud)

之后转换NSDataNSString......


Phi*_*hil 10

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